語法
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
-
語法:
test expr
或者[ expr ]
-
舉例:
-
if test 2 -gt 1; then echo 0; fi;
-
if [ 2 -gt 1 ]; then echo 0; fi;
-
-
多個
expr
可以用&&
或者||
連接,表示與
或或
數字的比較
-
-eq
或==
: is equal to -
-ne
或!=
: is not equal to -
-lt
: is less than -
-le
: is less than or equal to -
-gt
: is greater than -
-ge
: is greater than or equal to
字符串的比較
-
string1 = string2
: is equal to -
string1 != string2
: is not equal to -
string1
: is not null or not defined -
-n string1
: is not null and does exist -
-z string1
: string1 is null and does exist
文件和目錄
-
-s file
: not empty file -
-f file
: is file exist or normal file and not a directory -
-d dir
: is directory exist and not a file -
-w file
: is writeable file -
-r file
: is read-only file -
-x file
: is executable file
邏輯運算符
-
! expr
: not -
expr1 -a expr2
: and -
expr1 -o expr2
: or