The test command is very powerful, but it is difficult to satisfy its escape requirements and the difference between string and arithmetic comparisons.
Greater than |
Less than |
Equals |
Not equal to |
If ["$a"-gt "$b"] |
If ["$a"-lt "$b"] |
If ["$a"-eq "$b"] |
If ["$a"-ne "$b"] |
if [["$a"-gt "$b"]] |
if [["$a"-lt "$b"]] |
if [["$a"-eq "$b"]] |
if [["$a"-ne "$b"]] |
if (("$a" > "$b")) |
if (("$a" < "$b")) |
if (("$a" = "$b")) |
if (("$a"!= "$b")) |
String comparisons
Greater than |
Less than |
Equals |
Not equal to |
If ["$a" \> "$b"] |
If ["$a" \< "$b"] |
If ["$a" = "$b"] If ["$a" = "$b"] |
If ["$a"!= "$b"] |
if [["$a" > "$b"]] |
if [["$a" < "$b"]] |
if [["$a" = "$b"]] if [["$a" = "$b"]] |
if [["$a"!= "$b"]]
|
[[]] and []
Function |
[[ ]] |
[ ] |
Example |
String comparisons |
> |
\> |
- |
= (or = =) |
= |
- |
!= |
!= |
- |
An expression combination |
&& |
-A |
[[-N $var &&-F $var]] && echo "$var is a file" |
|| |
-O |
- |
Pattern matching (Pattern matching)
|
= (or = =) |
Wood has |
[[$name = "A *"]]-> the string "A *" |
Regular match (regularexpression matching) |
=~ |
Wood has |
[[$ (date) =~ ^fri\]] && echo "It s Friday the 13th!" |
-nt/-ot
Attribute |
|
Example |
File or folder exists |
-e |
[[ -e $config &NBSP;]]&NBSP;&&&N bsp;echo "config file exists: $config" |
File New and old comparison |
[[ $file 0 -nt $file 1 ]] && echo "$file 0 is newer than $file 1″ |
Same file |
-ef td> |
[[ $input -ef $output ]] \ && { echo "will not& nbsp;overwrite input file: $input "; exit 1; } |
Negative |
! |
- |
Some examples of
Logical combination judgments:
If [$condition 1] && [$condition 2]
If [$condition 1-a $condition 2]
If [$condi Tion1 && $condition 2]]
If [$condition 1] | | [$condition 2]
If [$condition 1-o $condition 2]
If [[$condition 1 | | | $condition 2]] # Also works.
#The &&, | | |, operators work within a [[]] test, despite giving an error within a [] construct.
Greater than |
Less than |
Equals |
Not equal to |
If ["$a"-gt "$b"] |
If ["$a"-lt "$b"] |
If ["$a"-eq "$b"] |
If ["$a"-ne "$b"] |
if [["$a"-gt "$b"]] |
if [["$a"-lt "$b"]] |
if [["$a"-eq "$b"]] |
if [["$a"-ne "$b"]] |
if (("$a" > "$b")) |
if (("$a" < "$b")) |
if (("$a" = "$b")) |
if (("$a"!= "$b")) |
String comparisons
Greater than |
Less than |
Equals |
Not equal to |
If ["$a" \> "$b"] |
If ["$a" \< "$b"] |
If ["$a" = "$b"] If ["$a" = "$b"] |
If ["$a"!= "$b"] |
if [["$a" > "$b"]] |
if [["$a" < "$b"]] |
if [["$a" = "$b"]] if [["$a" = "$b"]] |
if [["$a"!= "$b"]]
|
[[]] and []
Function |
[[ ]] |
[ ] |
Example |
String comparisons |
> |
\> |
- |
= (or = =) |
= |
- |
!= |
!= |
- |
An expression combination |
&& |
-A |
[[-N $var &&-F $var]] && echo "$var is a file" |
|| |
-O |
- |
Pattern matching (Pattern matching)
|
= (or = =) |
Wood has |
[[$name = "A *"]]-> the string "A *" |
Regular match (regularexpression matching) |
=~ |
Wood has |
[[$ (date) =~ ^fri\]] && echo "It s Friday the 13th!" |
attribute |
|
Example /strong> |
File or folder exists |
E |
[ -e ; $config ]] && echo "config file exists: $config" |
File old and new comparison |
-nt/-ot |
[[ $file 0 -nt $file 1 ]] && echo "$file 0 is newer than $file 1″ |
Same file |
-ef |
[[ $input -ef $output ]] \ && { echo "will not overwrite input file: $input "; exit 1; } |
Deny |
! |
- |
Some examples of
logical combination judgments:
If [$condition 1] && [$condition 2]
If [$condition 1-a $condition 2 ]
If [$condition 1 && $condition 2]]
If [$condition 1] | | [$condition 2]
If [$condition 1-o $condition 2]
If [[$condition 1 | | | $condition 2]] # Also works.
#The &&, | | |, operators work within a [[]] test, despite giving an error within a [] construct.