Test options and test commands for shell notes: Shell code #! /Bin/bash www.2cto.com echo file comparison operator echo operator Description Example echo-e filename if filename exists, it is true [-e/var/log/syslog] echo-d filename if filename is a directory, it is true [-d/tmp/mydir] echo-f filename. If filename is a regular file, it is true [-f/usr/bin/grep] echo-L filename. If filename is a symbolic link, true [-L/usr/bin/grep] echo-r filename if filename is readable, true [-r/var/log/syslog] echo-w filename if filename is writable, it is true [-w/var/mytmp.txt] echo- X filename if filename is executable, true [-L/usr/bin/grep] echo filename1-nt filename2 if filename1 is newer than filename2, the true [/tmp/install/etc/services-nt/etc/services] echo filename1-ot filename2 if filename1 is older than filename2, returns the true [/boot/bzImage-ot arch/i386/boot/bzImage] echo string comparison operator echo-z string. If the string length is zero, true [-z "$ myvar"] echo-n string if the string length is not zero, true [-n "$ myvar"] echo string1 = string2 if strin If g1 is the same as string2, it is true ["$ myvar" = "one two three"] echo string1! = String2 if string1 is different from string2, it is true ["$ myvar "! = "One two three"] echo arithmetic comparison operator echo num1-eq num2 equals [3-eq $ mynum] echo num1-ne num2 not equal to [3-ne $ mynum] echo num1-lt num2 less than [3 -lt $ mynum] echo num1-le num2 less than or equal to [3-le $ mynum] echo num1-gt num2 greater than [3-gt $ mynum] echo num1-ge num2 greater than or equal to [3-ge $ mynum] use the test command and [[] to perform more complex tests: shell code #! /Bin/bash www.2cto.com # test expr and [expr] are equivalent; $? Check the return value. You can use & | test 3-gt 4 & echo true | echo false #-eq,-ne,-lt,-le, -gt,-ge compares arithmetic values. # The operator = ,! =, <,> Compare whether the strings are equal, not equal, or the first string is sorted before or after the second string. # Use the single object operator-z to test the null String; # Use the \ <\> escape to escape the string because shell also uses the <> operator for redirection. ["Abc "! = "Def"]; echo $? ["Abc" \ <"AB"]; echo $? ["Abc" \> "abcd"]; echo $? The options www.2cto.com #-a AND-o allow the use of logical operators and or to combine expressions. Single Object Operator! Is not logical. # Shell usually needs to run the expressions in parentheses in the subshell. Therefore, use \ (\) to escape the parentheses, or enclose these operators in single or double quotation marks. # Test "a" = "a"-a 3-lt 4; echo $? Test ""! = "B"-a 3-ge 4; echo $? [! \ ("A" = "a"-o 3-lt 4 \)]; echo $? [! \ ("A" = "a"-o 3-lt 4 ")"]; echo $? # (And [[# (expr) composite command to calculate the arithmetic expression. If the expression evaluates to 0, it is set to 1; # If the evaluation value is not 0, set to 0. # You do not need to escape the operators. It can perform common arithmetic, logical, and bit operations in C language. # Arithmetic operations only apply to integers. Let x = 2 y = 2 ** 3 z = y * 3 echo $ x $ y $ z (w = y/x + ((~ ++ X) & 0xf); echo $? $ X $ y $ w # (w = y/x + ((~ ++ X) & 0xf); echo $? $ X $ y $ w # [[] can use a more natural syntax for file names and strings. [[(-D "'pwd'") & (-w "'pwd'")] & echo "'pwd' is writable directory" # Use = or! = When the operator is used, it matches with the command [[and can also perform pattern matching on strings. [["Abc def. d, x --" = a [abc] * \? D *]; echo pattern match $? [["Abc def c --" = a [abc] * \? D]; echo pattern match $? [["Abc def d, x" = a [abc] * \ d *]; echo pattern match $?