Binary comparison operators, comparing variables or comparing numbers.
Note the difference between a number and a string.
1. Integer comparison
[CPP]View PlainCopyprint?
- -eq equals, such as:if [ "$a"-eq " $b"]
- -ne not equal to, such as:if [ "$a"-ne " $b"]
- -GT greater than, such as:if [ "$a"-gt " $b"]
- -ge greater than equals, such as:if [ "$a"-ge " $b"]
- -lt less than, such as:if [ "$a"-lt " $b"]
- -le less than equals, such as:if [ "$a"-le " $b"]
- < less (requires double brackets), such as: (("$a" < "$b" )
- <= is less than or equal (requires double brackets), such as: (("$a" <= " $b"))
- > Greater than (requires double brackets), such as: (("$a" > "$b" )
- >= greater than or equal (requires double brackets), such as: (("$a" >= " $b"))
Small data comparisons can be used with awk
2. String comparisons
[CPP]View PlainCopy print?
- = equals, such as:if [ "$a" = "$b"]
- = = equals, such as:if [ "$a" = = " $b"], and = equivalent
Attention:
The way to compare two strings for equality is if ["$test" x = "Test" x]; Then here are the key points: 1 use a single equals sign 2 notice that there is a space on each side of the equal sign: This is the UNIX shell's requirement 3 notice that the "$test" x Last X, which is deliberately arranged, because when $test is empty, the expression above becomes x = Testx, is obviously not equal. Without this x, the expression would be an error: [: =: unary operator expected
Note: The behavior of the = = function is different in [[]] and [], as follows:
[CPP]View PlainCopyprint?
- [[$a = = z*]] # if $ A starts with "Z" (pattern match) then true
- [[$a = = "z*"]] # if $ A equals z* (character match) Then the result is true
- [$a = = z*] # File globbing and word splitting will occur
- [ "$a" = = "z*"] # if $ A equals z* (character match) Then the result is true
A little explanation, about file globbing is a shorthand for the document, such as "*.c" is, again like ~ also.
But file globbing is not a strict regular expression, although in most cases the structure is more like.
! = does not equal, such as: if ["$a"! = "$b"]
This operator will use pattern matching in the [[]] structure.
< less than, in ASCII alphabetical order. such as:
if [["$a" < "$b"]
If ["$a" \< "$b"]
Note: the "<" in the [] structure needs to be escaped.
> Greater than, in ASCII alphabetical order. such as:
if [["$a" > "$b"]
If ["$a" \> "$b"]
Note: the ">" in the [] structure needs to be escaped.
Refer to Example 26-11 for an example of this operator application.
The-Z string is "null". The length is 0.
-N string not "null"
Attention:
The use of-N in the [] structure must be used to test the variable. Use a string that is not ""! -Z
Or the string itself, which is not referenced by "", is placed in the [] structure. Although it is generally possible to
To work, but it's not safe. It's a good habit to use "" to test strings.
Example:
1. Comparison of numbers
[CPP]View PlainCopyprint?
- #!/bin/bash
- I=6
- a=10
- If [$a-eq 10]
- Then
- Echo "A = ten"
- Fi
- If [$a-ne $i]
- Then
- Echo "A! = $i"
- Fi
- If [$a-gt $i]
- Then
- echo "a > i"
- Fi
- If [$a-lt $i]
- Then
- Echo "A < I"
- Else
- echo "a > i"
- Fi
- if (("$a" > "$i")
- Then
- Echo "(()) a>i"
- Fi
- if ($a! = $i))
- Then
- Echo "(()) a!=i"
- Fi
Note: Run script through SH, [] operation is OK, and (()) Run error
chmod after 777, direct./Run, all can
2. String comparisons
[CPP]View PlainCopyprint?
- #!/bin/bash
- A="123"
- b="1234"
- c="123"
- If [ "$a" x! = "$b" x]
- Then
- Echo "A! = B"
- Fi
- If [ "$a" x = "$c" x]
- Then
- Echo "A = = C"
- Fi
Determine if the string is empty
[CPP]View PlainCopyprint?
- If [-Z "$d"]
- Then
- Echo "D is empty"
- Fi
Note:
-E File exists
-A file exists (deprecated)
-F test file is a regular file (normal file, non-directory or device)
-S file length is not 0
-D is the directory of the object being tested
-B The object being measured is a block device
-c The object being measured is a character device
-P The object being measured is the pipe
-h The measured file is a symbolic connection
-L test file is a symbolic connection
-S (uppercase) the file being measured is a socket
-T relates a file descriptor to an end device. The stdin[-t0] or [-T1] that is used to detect the script is a terminal
-r file has read permissions for the user running the script
-W file has write permissions for the user running the script
-X file has execute permissions for the user running the script
-U Set-user-id (SUID) flag to file, which is the root permission file that can be used by ordinary users, through chmod +s file implementation
-K Set Paste bit
-o The user who runs the script is the owner of the file
-G file Group-id the same as the user running the script
-N is read from the end of the file to the present, whether it is modified
F1-nt F2 file F1 is newer than F2
F1-ot F2 file F1 is older than F2
F1-ef f2 files F1 and F2 are hard-wired to the same file
Shell script----if (numeric condition, string condition, string null)