The command test in the shell is used to check whether a condition is true. It can test numbers, strings and files.
Number arguments test
Integer1 and Integer2 variables are algebraically equal
Integer1 and Integer2 variables are not algebraically equal
Integer1 is greater than Integer2
Integer1 is greater than or equal to Integer2
Integer1 is lower than Integer2
Integer1 is lower than or equal to Integer2
if test $[num1] -eq $[num2]
echo 'number 1 is equal to number 2！'
echo 'number 1 is not equal to number 2！'
number 1 is equal to number 2！
 In the code performs basic arithmetic operations, such as:
result=$[a+b] # Note that there can be no spaces on both sides of the equal sign
echo "result is： $result"
result is： 29
String arguments test
String1 and String2 variables are equal
String1 and String1 variables are not equal
Check the length of the string is zero
Check the length of the string is not zero
if test $num1 = $num2
echo 'String1 and String2 variables are equal !'
echo 'String1 and String2 variables are not equal!'
String1 and String2 variables are not equal!
File arguments test
FILE exists and read permission is granted
FILE exists and write permission is granted
FILE exists and execute permission is granted
FILE exists and has a size greater than zero
FILE exists and is a directory
FILE exists and is a regular file
FILE exists and is character special
FILE exists and is block special
if test -e ./bash
echo 'FILE exists!'
echo 'FILE does not exist!'
FILE does not exist!
In addition, shell also provides three logical operators (-a), or (-o), and not (!) which be used to connect the test conditions, and its priority is: “!” is the highest, “-a” is the second, “-o” is the lowest.
if test -e ./notFile -o -e ./bash
echo 'At least one file exists!'
echo 'Two files exist'