If condition test common syntax
- Test < testing expressions >
- [< test expression >]
- [[< Test expression >]]
- (< test expression >))
Test command AND [] testing operator
- Test < testing expressions >
- [< test expression >]
(these two syntaxes are equivalent)
| test operator |
full Spell |
Description |
| -D |
Directory |
File exists and is directory |
| -F |
File |
File exists and is a normal file |
| -E |
Exist |
File exists |
| -R |
Read |
File exists and is readable |
| -S |
Size |
File exists and file size is not 0 |
| -W |
Write |
File exists and can be written |
| -X |
Exexutable |
File exists and is executable |
| -L |
Link |
File exists and is a linked file |
| F1-nt F2 |
Newer than |
File f1 than file F2 new |
| F1-ot F2 |
Olderthan |
File F1 F2 older than file |
| -N |
Not zero |
The length of the string is not 0 |
| -Z |
Zero |
The length of the string is 0 |
| S1 = s2 |
|
String 1 equals String 2 |
| S1! = S2 |
|
String 1 is not equal to string 2 |
| -eq |
Equal |
Equal |
| -ne |
Not equal |
Not equal |
| -gt |
Greater than |
Greater than |
| -ge |
Greater equal |
Greater than or equal |
| -lt |
Less than |
Less than |
| -le |
Less equal |
Less than or equal |
| -A |
and |
And |
| -O |
Or |
Or |
| ! |
Not |
Non - |
Example:
test -f /data/test.sh && echo true || echo false
[ -f /data/test.sh ] && echo true || echo false
- When testing a variable with [], the test result may be incorrect if the variable being tested does not have double quotes:
file1 = / etc / passwd
[-f "$ file1"] && echo 1 || echo 0
If it is a file entity path, the result of quoting is the same as unquoting:
[-f "/ etc / passwd"] && echo 1 || echo 0
[-f / etc / passwd] && echo 1 || echo 0
[[]] test operator
In [[]] you can use wildcards, etc. for pattern matching
[[! $ a = ~ [1-3]]] && echo 1 || echo 0
| test operator |
full Spell |
Description |
| -D |
Directory |
File exists and is directory |
| -F |
File files exist and are normal files |
| -E |
Exist |
File exists |
| -R |
Read |
File exists and is readable |
| -S |
Size |
File exists and file size is not 0 |
| -W |
Write |
File exists and can be written |
| -X |
Exexutable |
File exists and is executable |
| -L |
Link |
File exists and is a linked file |
| F1-nt F2 |
Newer than |
File f1 than file F2 new |
| F1-ot F2 |
Olderthan |
File F1 F2 older than file |
| -N |
Not zero |
The length of the string is not 0 |
| -Z |
Zero |
The length of the string is 0 |
| S1 = s2 |
|
String 1 equals String 2 |
| S1! = S2 |
|
String 1 is not equal to string 2 |
| ==/= |
Equal |
Equal |
| != |
Not equal |
Not equal |
| > |
Greater than |
Greater than |
| >= |
Greater equal |
Greater than or equal |
| < |
Less than |
Less than |
| <= |
Less equal |
Less than or equal |
| && |
and |
And |
| || |
Or |
Or |
| ! |
Not |
Non - |
(()) test operator
- (< test expression >))
- Common use and calculation
- For relational operations of integers, you can also use the Shell's Arithmetic operator (())
((3>2)) && echo 1 || echo 0
((3!=2&&5==5)) && echo 1 || echo 0
07-shell If condition test and comparison