Linux shell 条件表达式
时间:2010-05-04 来源:大方无隅
Conditional judge
the result of conditional judge is true or false.
0 --- true and Non-0 --- false
test cond_expr or [ cond_expr ]
cond_expr:
file type and the permission of the file
string content and the length
compare values
Expression
expression If expression is true, !expression is false
expression1 –a expression2 Both expression1 and expression2 are true
Expression1 –o expression2 Either expression1 or expression2 are true
String
-n string The length of string is nonzero
-z string The length of string is zero
string1 = string2 The strings are equal
string1 != string2 The strings are not equal
string1 The string1 is not null
Integer
interger1 –eq interger2 Interger1 is equal to interger2
interger1 –ge interger2 Interger1 is greater than or equal to interger2
interger1 –gt interger2 Interger1 is greater than interger2
interger1 –le interger2 Interger1 is less than or equal to interger2
interger1 –lt interger2 Interger1 is less than interger2
interger1 –ne interger2 Interger1 is not equal to interger2
File
file1 –ef file2 file1 and file2 have the same device and inode number
file1 –nt file2 file1 is newer(modification date) than file2
file1 –ot file2 file1 is older than file2
-b –c –d file file exits and is block/character/directory type
-e file exits
-s file exists and the length bigger than 0
-f file is a normal file
-r –w -x file exits and is readable/writable/executable
-L -h file exists and is a symbolic link