2014-02-15 23 views
0

我需要寫,如果另一里面如果是的樣子:怎樣寫,如果內部另一如果在bash腳本語言

if(condition) 
{ 
      if(condition) 
    { 
     //do something 
    } 

    //do something 
} 

如何書寫,但在bash腳本語言?

+2

也許你想從一個[Unix Shell Scripting Tutorial]開始(http:// support web.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html)? – Carpetsmoker

+0

http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Sonstructs – rici

+1

這將幫助你:[Bash入門指南](http://www.tldp.org/LDP/Bash -Beginners-Guide/html/sect_07_01.html) – bprayudha

回答

1

結構爲:

if [ condition ]; then 
    if [ condition ]; then 
    #Do something                                 
    elif [ condition ]; then 
     #Do something                                 
    fi 
else 
    #Do something                                  
fi 

您也可以用&&(AND)查詢條件在一起,並||(OR) 和許多其他的事情就知道...

你可以從HEREHERE

+1

@Akari我用兩個有用的鏈接更新了答案:) – MLSC

3

使用if/then構造的條件測試可能是嵌套的。最終結果等效於使用複合比較運算符。

a=3 

if [ "$a" -gt 0 ] 
then 
    if [ "$a" -lt 5 ] 
    then 
    echo "The value of \"a\" lies somewhere between 0 and 5." 
    fi 
fi 

# Same result as: 

if [ "$a" -gt 0 ] && [ "$a" -lt 5 ] 
then 
    echo "The value of \"a\" lies somewhere between 0 and 5." 
fi