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