shell條件判斷

來源:互聯網
上載者:User

shell條件判斷
1. if條件判斷if條件判斷: if [ 條件 ]; then do something fi 多個條件: && 代表AND || 代表OR 多重判斷: if [ 條件 ];then do something elif [ 條件2 ]; then //do something else //do someshing fi

#!/bin/bashread -p "Please input (Y/N) : " ynif [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then     echo "Yes!"elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then     echo "No!"else    echo "Input Error"fiexit 0
查看運行結果
[work@www sh]$ sh hello.sh Please input (Y/N) : yYes![work@www sh]$ sh hello.sh Please input (Y/N) : lInput Error[work@www sh]$
2. case判斷如果if中判斷層級比較多,建議使用case, 否則嵌套太多,代碼易讀性變差。 case的文法 case $變數名稱 in “變數1”) do something ;; 每個結尾用兩個分號 “變數2”) do something ;; *) 最後一個*代表其他值 do something ;; esac 看下面例子:
#!/bin/bashcase $1 in    "one")        echo "One"        ;;    "two")        echo "Two"        ;;    *)        echo "Usage $0 (one|two)"        ;;esacexit 0
運行結果
[work@ww sh]$ sh hello.sh Usage hello.sh (one|two)[work@www sh]$ sh hello.sh oneOne[work@www sh]$
地址:http://blog.csdn.net/yonggang7/article/details/40480695

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.