Linux Gvim shell if...else語句

#if else語句,共分三種:if...fi ; if ...else ...fi ; if ...elif...else...fi
#if else fi 語句語法
#if [ expression ]
#then
#    Statement to be executed if expression is true
#else
#    Statement to be executed if expression is not true
#fi
#if elif else fi語句語法:哪個expression爲true就執行哪個語句,若是全是false則執行else語句
#if [ expression 1 ]
#then
#   Statement(s) to be executed if expression 1 is true
#elif [ expression 2 ]
#then
#   Statement(s) to be executed if expression 2 is true
#elif [ expression 3 ]
#then
#   Statement(s) to be executed if expression 3 is true
#else
#   Statement(s) to be executed if no expression is true
#fi
express

 1 a=30
 2 b=20
 3 if [ $a == $b ]
 4 then
 5    echo "a is equal to b"
 6 elif [ $a -gt $b ]
 7 then
 8    echo "a is greater than b"
 9 elif [ $a -lt $b ]
10 then
11    echo "a is less than b"
12 else
13    echo "None of the condition met"
14 fi

#if else 語句也能夠寫成一行,以命令的方式運行,以下:less

1 if test $[ 2*3 ] -eq $[ 1+5 ] ;then echo "true" ; fi

#if else 語句也常常與test命令結合使用
#test命令用於檢查某個條件是否成立與 []相似spa

1 num1=$[2*3]
2 num2=$[1+5]
3 if test ${num1} -eq ${num2}
4 then
5     echo 'The two numbers are equal!'
6 else
7         echo 'The two numbers are not equal!'
8 fi
相關文章
相關標籤/搜索