Conditional test Syntax:
Syntax 1:test < Test expressions > This is the method of conditional test expressions with the test command.
Syntax 2:[< test expressions >]: This is the way to test an expression with a single parenthesis, as with the use of the test command, the recommended
Syntax 3:[[< test expression >]: This is the method of testing the expression by [[]], which is more than the syntax format of test and [] Update
Syntax 4: ((<c Test expression >)): This is a method of conditional expressions (()), typically used in an if statement
For the instructions above:
Syntax 1 and Syntax 2 are the same, and syntax 3 is an extension of test. Recommended Syntax 2
You can use wildcard characters for pattern matching in [[]], which is where the difference is with several other syntax formats
&&,| |,>,< and other operators can be applied with [[]], but cannot be applied with [], in [] generally with-a,-o,-get (for integers),-lt (for integers) instead of the above operator.
For relational operators of integers, you can also use the arithmetic operator (())
[Email protected]:/home/xpg# test-f/home/xpg/match.sh && echo Good | | Echo Badgood
Example: Use-Z to test a string, if the length of the string is 0 returns true,-N instead
[Email protected]:/home/xpg# char=oldboy[email protected]:/home/xpg# test-z "$char" && echo 1 | | echo 00[email protected]:/home/xpg# char=[email protected]:/home/xpg# test-z "$char" && echo 1 | | Echo 01[email protected]:/home/xpg#
[] Like the test nature, this is used primarily in the work.
[Email protected]:/home/xpg# [-f/home/xpg/match.sh] && echo 1 | | echo 01 is the same in nature [email protected]:/home/xpg# [-f/home/xpg/match.sh] && Echo 1 1[email protected]:/home/xpg# [- F/HOME/XPG/MATCH.SH] | | echo 0 [Email protected]:/home/xpg#
Because the test expression and [] are the same, you can use man test to view the
[Email protected]:/home/xpg# [[-f/home/xpg/match.sh]] && echo 1 | | Echo 1
1
[[]] and [] The difference is that [[]] can use wildcards and so on pattern matching, and &&,| |,>,< and other operators can be applied in [[]], but not applied in [], in [] generally use-A,-O,-GT,-LT instead of the above [[]] .
File test expression
Let's add one thing:
Str1! = str2 when the string str1 and str2 are equal to True-n str1 when the length of the string is greater than 0 is true (string non-null)-Z str1 when the string length is 0 true (empty string) str1 when the string str1 is non-empty For real! STR2 true if the substring is empty int1-eq int2 two number equal to True Int1-ne Int2 two number is true INT1-GT Int2 int1 greater than Int2 for true Int1-ge Int2 Int1 greater than or equal to Int2 true int1-lt int2 int1 less than Int2 for true Int1-le int2 int1 less than equals Int2 true
-D: The file file exists and is true for the directory
-F: File file exists and is true for normal files
-E: File file exists is true, and the difference between the above-d,-f is not distinguished is the directory or file
-r: File file exists and readable is true
-S: File file is present and size is not 0 true
-W: File file exists and can be written as true
-x: File file exists and can be executed as true
-L: File file exists and is true for connection file
F1-nt F2, NT full spell for newer than file F1 than file F2 new is true.
F1-ot F2;-ot full spell for older than file F1 than file F2 old is true
Use-e and-D combined: [email protected]:/home/xpg# [-e/home/xpg/match.sh-a-d/home/xpg/match.sh] && echo 1 | | echo 00
Startup scripts for production
For example:
[Email protected]:/home/xpg# [-x/usr/bin/apache2] | | Exit 5 #简单的判断, if the file does not exist or cannot be executed, return to exit 5exit[email protected]:~$ Echo $?5[email protected]:~$
Write simple and efficient test files
When doing test judgment, it is not necessary to follow the "previous operation succeeded how, otherwise how" method, sometimes directly partial judgment, sometimes looks more simple.
[Email protected]:/home/xpg# [-x XPG] && echo 1[email protected]:/home/xpg# [-x XPG] | | Echo 1 1
Conditional testing and comparison of shell scripts