shell scripts and test conditions for comparison

1, commonly used grammar test conditions

test test expression Use test commands conditional test expression, at least one space between the command and the test expression test
[Test expression] Test conditions for expression by [] brackets, [] at least one space between the boundary and the test expression in brackets
[[Test expression]] The condition is tested by expression [[]] double brackets, [[]] at least one space between the double brackets test expression
((Test expression)) By (()) bis test expression conditions parentheses, ()) bis ends parentheses space is not required, commonly used in Comparative integer

 Description:

  • Double brackets [[]] may be a wildcard match, which is different from that of several other places grammar
  • &&, ||, <,> operator can be used like double brackets [[]] in, but not to [], the general use -a, -o, -lt, -gt instead in []

For example:

1 test -f /tmp/test.txt && echo 1 || echo 0
2 [ -f /tmp/test.txt ] && echo 1 || echo 0
3 [[ -f /tmp/test.txt ]] && echo 1 || echo 0
4 ((3>2)) && echo 1 || echo 0

2, the test expression usage

2.1, file test expression

-d file File exists and is a directory was true
-f file File exists and is a regular file True
-e file True file exists, is not figuring directory or file
-s file File exists and the file size is not 0 True
-r file True exists and is readable, and execution of the script file is also related to user rights
-w file File exists and is writable True, with user permission to execute the script also related
-x file True file exists and is executable, and user permission to execute the script also related
-L file File exists and is linked files True
f1 -nt f2 True New file f1 f2 than the file modification time is calculated based on the file
f1 f2 -ot  File f1 true, according to the file modification time than file f2 old was calculated

 

2.2, the string test expression

-n "string" If the string length is not 0, True
-z "string" If the length of the string of 0 True
"串1" = "串2" If the string is equal to 1 True string 2, it can be used "==" instead of "="
"串1" != "串2" If the string is not equal to 1 True 2 strings available! "==" instead of "! ="

note:

  • For string comparison, definitely more than you want to add quotation marks after the string. Such as [-n "$ string"]
  • = And! = String can be used to determine whether two identical

 

2.3, the integer operator

In [] and the test In [[]] (()) and Explanation
-eq == or = Equal, spelling is equal
-nq != Is not equal, spelling is not equal
-gt > Is greater than, is greater than spelling
-give >= Greater than or equal, is greater equal spelling
-lt < Less than, is less than spelling
-the <= Or less, is less equal spelling

 

 2.4 Logical Operators

In [] and the test In [[]] (()) and Explanation
-a && and, with both ends are true, the result is true
-O || or, or, both ends is true, the result is true
! ! Not, non, the opposite ends, the result is true

 

2.5, the test expression differences summary

Test expression symbol test [ ] [[ ]] (( ))
Whether boundary space is required need need need Do not need
Logical Operators !,-to !,-to !、&&、|| !、&&、||
Integer Comparison operators -eq, -ne, -lt, -gt, -ge, -the -eq, -ne, -lt, -gt, -ge, -the

-eq, -ne, -lt, -gt, -ge, -le or

=、!=、<、>、>=、<=

=、!=、<、>、>=、<=
String comparison operators =、==、!= =、==、!= =、==、!= =、==、!=
Supports wildcards not support not support stand by not support

Guess you like

Origin www.cnblogs.com/hovin/p/11244586.html