Shell Programming--if Statements

Source: Internet
Author: User

Format 1:if condition; Then statement; Fi
Script if1.sh

[[email protected] shell]# vim if1.sh#/bin/basha=5if [ $a -gt 3 ]  then       echo okfi

Execution results

[[email protected] shell]# sh -x if1.sh+ a=5+ ‘[‘ 5 -gt 3 ‘]‘+ echo okok

Format 2:if condition; Then statement; else statement; Fi
Script if2.sh

[[email protected] shell]# vim if2.sh#/bin/basha=2if [ $a -gt 3 ]  then       echo ok  else       echo nookfi

Execution results

[[email protected] shell]# sh -x if2.sh+ a=2+ ‘[‘ 2 -gt 3 ‘]‘+ echo nooknook

Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
Script if3.sh

[[email protected] shell]# vim if3.sh#/bin/basha=6if [ $a -gt 9 ]then    echo okelif [ $a -gt 3 ]then    echo "3<a<9"else    echo nookfi

Execution results

[[email protected] shell]# sh -x if3.sh+ a=6+ ‘[‘ 6 -gt 9 ‘]‘+ ‘[‘ 6 -gt 3 ‘]‘+ echo ‘3<a<9‘3<a<9

can use && | | Combine multiple conditions
Script if4.sh

[[email protected] shell]# vim if4.sh#/bin/basha=6if [ $a -lt 9 ] && [ $a -gt 3 ]then    echo "3<a<9"else    echo nookfi

Execution results

[[email protected] shell]# sh -x if4.sh+ a=6+ ‘[‘ 6 -lt 9 ‘]‘+ ‘[‘ 6 -gt 3 ‘]‘+ echo ‘3<a<9‘3<a<9

Script if5.sh

[[email protected] shell]# vim if5.sh#/bin/basha=2if [ $a -gt 9 ] || [ $a -lt 3 ]then    echo "a<3 or a>9"else    echo nookfi

Execution results

[[email protected] shell]# sh -x if5.sh+ a=2+ ‘[‘ 2 -gt 9 ‘]‘+ ‘[‘ 2 -lt 3 ‘]‘+ echo ‘a<3 or a>9‘a<3 or a>9

Shell Programming--if Statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.