Shell if else語句

來源:互聯網
上載者:User

標籤:style   blog   color   io   使用   ar   sp   div   on   

if 語句通過關係運算子判斷運算式的真假來決定執行哪個分支。Shell 有三種 if ... else 語句:

  • if ... fi 語句;
  • if ... else ... fi 語句;
  • if ... elif ... else ... fi 語句。
1) if ... else 語句

if ... else 語句的文法:

if [ expression ]then   Statement(s) to be executed if expression is truefi

如果 expression 返回 true,then 後邊的語句將會被執行;如果返回 false,不會執行任何語句。

最後必須以 fi 來結尾閉合 if,fi 就是 if 倒過來拼字,後面也會遇見。

注意:expression 和方括弧([ ])之間必須有空格,否則會有語法錯誤。

#!/bin/sha=10b=20#下面兩種寫法都是正確的,除此之外的寫法都是錯誤的if [ $a == $b ]then    echo ‘a is equal to b‘fiif [ $a != $b ] ; then    echo ‘a is not equal to b‘fi

運行結果:

a is not equal to b
2) if ... else ... fi 語句

if ... else ... fi 語句的文法:

if [ expression ]then   Statement(s) to be executed if expression is trueelse   Statement(s) to be executed if expression is not truefi

如果 expression 返回 true,那麼 then 後邊的語句將會被執行;否則,執行 else 後邊的語句。

舉個例子:

#!/bin/sha=10b=20if [ $a == $b ] ;then   echo "a is equal to b"else   echo "a is not equal to b"fiif [ $a == $b ]then    echo "a is equal to b"else    echo "a is not equal to b"fi:<<EOF 下面的寫法是錯誤的,請注意if [ $a == $b ] then   echo "a is equal to b"else     echo "a is not equal to b"fiEOF

執行結果:

a is not equal to ba is not equal to b
3) if ... elif ... fi 語句

if ... elif ... fi 語句可以對多個條件進行判斷,文法為:

if [ expression 1 ]then   Statement(s) to be executed if expression 1 is trueelif [ expression 2 ]then   Statement(s) to be executed if expression 2 is trueelif [ expression 3 ]then   Statement(s) to be executed if expression 3 is trueelse   Statement(s) to be executed if no expression is truefi

哪一個 expression 的值為 true,就執行哪個 expression 後面的語句;如果都為 false,那麼不執行任何語句。

舉個例子:

#!/bin/sha=10b=20if [ $a == $b ] ; then   echo "a is equal to b"elif [ $a -gt $b ] ; then    echo "a is greater than b"elif [ $a -lt $b ] ; then    echo "a is less than b"else    echo "None of the condition met"fi

運行結果:

a is less than b

if ... else 語句也可以寫成一行,以命令的方式來運行,像這樣:

#!/bin/bashif test $[2*3] -eq $[1+5]; then echo ‘The two numbers are euqal!‘; fi;
#!/bin/bashif test $(2*3) -eq $(1+5); then echo ‘The two numbers are euqal!‘; fi;

 

if ... else 語句也經常與 test 命令結合使用,如下所示:

 

#!/bin/bashnum1=$[2*3]num1_1=$[2*6]num2=$[1+5]num2_1=$[1+5]if test $[num1] -eq $[num2] ; then    echo "The two numbers are euqal!"else    echo ‘The tow numbers are not equal!‘fiif test $num1_1 -eq $num2_1 ; then    echo "The two numbers are euqal!"else    echo ‘The tow numbers are not equal!‘fi

輸出:

The two numbers are euqal!The tow numbers are not equal!

test 命令用於檢查某個條件是否成立,與方括弧([ ])類似。

Shell if else語句

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.