Conditional testing and comparison of shell scripts
First, the condition test of shell script
In general, various tests are performed in the various conditional structures and process control structures of bash, and then different operations are performed based on the test structure, sometimes combined with conditional statements such as if, to complete the test judgment to reduce errors in program operation.
When a conditional test expression is executed, it usually returns true or false, and the value returned after the execution of the command is 0 for true, not 0 for false.
Common syntax for conditional testing in Bash programming:
conditional test syntax |
description |
syntax one:test< test expression; |
|
syntax two:[< test expression ] |
This is a method of conditionally testing an expression through [] (single-square brackets), the same as the use of the test command. [] has at least one space between the boundary and the content; |
syntax three:[[< test expression;] |
This is a way to test an expression by using [[]] (double brackets). is the syntax format that is newer than test and []. [[]] has at least one space between the boundary and the content; |
syntax Four: ((< test expression >)) |
This is a method of conditional test expressions through (()) (double parenthesis), typically used in an if statement. (()) (double parenthesis) No spaces are required at both ends; |
There are a few things to note for the table above:
The test command in syntax one and the [] in syntax two are equivalent. Syntax three kinds of [[]] for the extended Test command, Syntax Four (()) Common term calculation, it is recommended to use a relatively friendly syntax two, that is, the syntax of the brackets [] format;
in [[]] can use wildcard characters and so on pattern matching, this is different from the other syntax format of the place;
The &&, | |, >, <\ and other operators can be applied to [[]], but not in [], in [] generally with-a,-O,-gt (for integers),-lt (for integers) instead of the above operator;
For a relational operation of integers, you can also use the Shell's Arithmetic operator (());
Example:
Test-f file && echo true | | echo False
Second, the file test expression
Common file Test operators |
Description |
-D files, all of which are spelled as directory |
The file exists and the directory is true, that is, the test expression is established; |
-f file, F's full spell file |
The file exists and is the normal file is true, that is, the test expression is established; |
-e file, the full spell for exist |
The existence of the file is true, that is, the test expression is established. Different from "-F", "-e" can not tell whether it is a directory or a file; |
-R file, R's full spell read |
The file exists and the readable test is true, that is, the test expression is established; |
-S file, S is fully spelled as size |
The file exists and the file size is not 0 is true, that is, the test expression is established; |
-W file, W's full spell is write |
The file exists can be written as true, that is, the test expression is established; |
-x file, the full spell for executable |
The file exists and is executable as true, that is, the test expression is established; |
-l file, L's all spelled as Link |
The file exists and the linked file is true, that is, the test expression is established; |
F1-nt F2,nt's full spell is newer than |
The file F1 is F2 new than the file is true, that is, the test expression is established. Calculated according to the time of modification of the document; |
F1-ot F2,nt's full spell is older than |
The file F1 is true than the file F2 old, that is, the test expression is established. Calculated according to the time of modification of the document; |
These operation symbols are almost universal for test expressions [[]], [], and tests.
Three, character test expression
The function of the string test operator is to compare whether two strings are the same, whether the length of the test string is zero, whether the string is null, and so on;
Common string test operator |
|
-n "string" |
|
-z "string" |
|
" string 1 "= String 2 ' |
" string 1 "! = "String 2" |
|
The following is a hint for the string test operator:
- For a string test, be sure to double-quote the string after the comparison. such as [-N $myvar], especially the use of [] scene;
- The comparison symbol (for example = and! =) must have spaces on both ends;
- "! =" and "=" can be used to compare whether two strings are the same;
Example:
[[Email protected] ~]# [-N "abc"] && Echo 1 | | echo 0 #如果字符串长度不为0, then output 1, otherwise output 0;
#因为字符串为abc, the length is not 0, therefore true, output 1;
Four, integer two-dollar comparison operator
comparison symbols used in [] and test |
Comparison symbols used in (()) and [[]] |
Description |
-eq |
= = or = |
Equal, full spell for equal |
-ne |
!= |
Unequal, all spelled as not equal |
-gt |
> |
Greater than, full spell for gerater than |
-ge |
>= |
Greater than or equal, full spell for gerater equal |
-lt |
< |
Less than |
-le |
<= |
is less than or equal, and is all spelled as lesser equal |
The following are special instructions for the above symbols:
1, "=" and "! =" can also be used in [], but in [] using symbols containing ">" and "<" need to be escaped with a backslash, sometimes not escaped although the syntax will not error, but the results may be incorrect;
2. You can also use symbols containing "-gt" and "-lt" in [[]], but it is not recommended to use them;
3, the comparison symbol at both ends should also have a space;
Example:
[[Email protected] ~]# [2 = 1] && echo 1 | | Echo 0
0
[[Email protected] ~]# [2 \< 1] && echo 1 | | Echo 0
0
Five, logical operators
operators used in [] and test |
operators used in [[]] and (()) |
description |
-a |
&& |
and, with, both ends are true, then the result is true |
-o |
P>| | |
or, or if one of the two ends is true, the result is true |
! |
! |
not, Non, opposite ends, the result is true |
For the above operators, there are the following hints:
- The expression before and after the logical operator is established, it is usually expressed by true or false;
- "!" The Chinese meaning is counter, that is, the logical value opposite to a logical value;
- The Chinese meaning of-A is "with" (and or &&), before and after two logical values are "true", the combined return value is "true" and vice versa "false";
- The Chinese meaning of-O is "or" (or or | |), and the return value is "true" if one of the 2 logical values is "true";
- Connection two expressions with [], test or [[]] are available && or | |;
Example:
[[Email protected] ~]# [-f/etc/hosts-a-f/etc/services] && echo 1 | | Echo 0
1
[[Email protected] ~]# [[-f/etc/hosts &&-f/etc/services]] && echo 1 | | Echo 0
1
Description: Operation rules for-A and &&: only the expression at both ends of the logical operator is true; True (true) indicates that the corresponding number is 1, False (false) indicates not, and the corresponding number is 0.
Vi. Summary of differences between test-expression tests, [], [[]], (())
To test an expression operator |
[] |
Test |
[[]] |
(()) |
Whether the boundary requires spaces |
Need |
Need |
Need |
Don't need |
logical operators |
!、-A,-O |
!、-A,-O |
!, &&, | | |
!, &&, | | |
Integer comparison Operators |
-eq,-GT,-lt,-ge,-le |
-eq,-GT,-lt,-ge,-le |
-eq,-GT,-lt,-ge,-le or =, >, <, >=, <= |
=, >, <, >=, <= |
string comparison operators |
=, = =,! = |
=, = =,! = |
=, = =,! = |
=, = =,! = |
Whether wildcard matches are supported |
Not supported |
Not supported |
Support |
Not supported |
Conditional testing and comparison of shell scripts