SoFunction
Updated on 2025-03-10

Summary of judging conditions of linux scripts (must read)

As shown below:

-b file If the file exists and is a block special file, it is true
-c file If the file exists and is a special character file, it is true
-d file If the file exists and is a directory, it is true
-e file If the file exists, it is true
-f file If the file exists and is a rule file, it is true
-g file If the file exists and the value of the SGID bit is set, it is true
-h file If the file exists and is a matching link, it is true
-k file If the file exists and the value of the "sticky" bit is set
-p file If the file exists and is a named pipe, then true
-r file If the file exists and is readable, it is true
-s file True if the file exists and its size is greater than zero
-u file If the file exists and the SUID bit is set, it is true
-w file If the file exists and is writable, it is true
-x file If the file exists and is executable, then true
-o file If the file exists and is owned by a valid user ID, then true

-z string If the string length is 0, it is true
-n string If the string length is not 0, it is true
string1 = string2 If two strings are equal, it is true
string1 != string2 If the two strings are not equal, then it is true

int1 -eq int2 If int1 is equal to int2, it is true
int1 -ne int2 If int1 is not equal to int2, it is true
int1 -lt int2 If int1 is less than int2, it is true
int1 -le int2 If int1 is less than or equal to int2, then it is true
int1 -gt int2 If int1 is greater than int2, it is true
int1 -ge int2 If int1 is greater than or equal to int2, then it is true

!expr If expr is false, the compound expression is true. expr can be any valid test expression
expr1 -a expr2 If both expr1 and expr2 are true, the whole formula is true
expr1 -o expr2 If one of expr1 and expr2 is true, the whole formula is true

The above summary of the judgment conditions for linux scripts (must read) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.