Shell中的條件判斷

原文鏈接:http://www.freeos.com/guides/lsst/ch03sec02.html
語法
if condition
then 
    command1 if condition is true or if exit status of condation is 0
fi
if condition
then
    condition is zero (true - 0) execute all commands up to else statement
else
    if condition is not true then execute all commands up to fi
fi
if condition
then
    condition is zero (true - 0) execute all commands up to elif statement
elif condition1 
then
    condition1 is zero (true - 0) execute all commands up to elif statement  
elif condition2
then
    condition2 is zero (true - 0) execute all commands up to elif statement          
else
    None of the above condtion,condtion1,condtion2 are true (i.e.  all of the above nonzero or false) execute all commands up to fi
fi
condition
  1. 語法: test expr或者[ expr ]

  2. 舉例:

    1. if test 2 -gt 1; then echo 0; fi;

    2. if [ 2 -gt 1 ]; then echo 0; fi;

  3. 多個expr可以用&&或者||連接,表示

數字的比較
  1. -eq==: is equal to

  2. -ne!=: is not equal to

  3. -lt: is less than

  4. -le: is less than or equal to

  5. -gt: is greater than

  6. -ge: is greater than or equal to

字符串的比較
  1. string1 = string2: is equal to

  2. string1 != string2: is not equal to

  3. string1: is not null or not defined

  4. -n string1: is not null and does exist

  5. -z string1: string1 is null and does exist

文件和目錄
  1. -s file: not empty file

  2. -f file: is file exist or normal file and not a directory

  3. -d dir: is directory exist and not a file

  4. -w file: is writeable file

  5. -r file: is read-only file

  6. -x file: is executable file

邏輯運算符
  1. ! expr: not

  2. expr1 -a expr2: and

  3. expr1 -o expr2: or

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章