(a) Conditions to determine if the meaning of-Z to-D
[-A file] is true if file exists.
[-B file] True if file exists and is a block special file.
[-C file] True if file exists and is a special file of a word .
[-D file] True if file exists and is a directory .
[-E File] True if file exists .
[-F file] True if file exists and is a normal file .
[-G file] True if file exists and Sgid has been set.
[-H file] True if file exists and is a symbolic connection.
[-K file] True if file exists and the sticky bit has been set.
[-P file] True if file exists and is a pipe.
[-R File] True if file exists and is readable.
[-S file] True if file exists and the size is not 0.
[-T FD] true if the file descriptor FD is open and points to a terminal.
[-u file] True if file exists and suid is set.
[-W file] True if file exists and is writable.
[-X file] True if file exists and is executable .
[-o file] True if file exists and is a valid user ID.
[-G file] True if file exists and is a valid user group.
[-L file] True if file exists and is a symbolic connection.
[-S file] True if file exists and is a socket.
(ii) Condition testing
Use the "test" or "[]" command :
If Test-f file.c
Then ...
Fi
Or:
If [-F file.c]
Then ...
Fi
Note : When you use square brackets "[]" to test the condition, the square brackets leave a space between the test conditions.
Test condition Classification:
string comparison class \ Arithmetic comparison class \ file-related condition test class
1. String comparison class:
string1 = string2 If two strings are the same , the result is true
String1! = string2 If the two strings are different, the result is true
- N String If the string is not empty , the result is true
- Z String If the string is empty , the result is true
note that when comparing equal or unequal, both sides of the =/!= must have a space character separated from the two strings to be compared.
2. Arithmetic Comparison class:
Expression1–eq expression2 #如果两个表达式equal , the result is true Expression1–ne expression2 #如果两个表达式range , The result is true expression1–gt expression2 #如果前一个表达式 is greater than the last expression, the result is true Expression1–ge expression2 #如果前一个表达式greater than or equal to After an expression, the result is true expression1–lt expression2 #如果前一个表达式less than the last expression, the result is true Expression1–le expression2 #如果前一个表达式 Less than or equal to the latter expression, the result is true! Expression #取表达式的非, which is false , the result is true
3. file Comparison class:
[File1-nt FILE2] If FILE2 is older than FILE1, or if FILE1 exists and FILE2 does not true.
[File1-ot FILE2] If FILE1 is older than FILE2, or FILE2 exists and FILE1 does not exist, it is true.
[File1-ef FILE2] True if FILE1 and FILE2 point to the same device and node number.
Conditions in the shell determine if and test