Original: http://apps.hi.baidu.com/share/detail/31263915
In the shell, the comparison between the string and the numerical method is different, pay attention to distinguish
Integer comparison:
-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"))
string comparison:
= equals, such as: if ["$a" = "$b"]
= = equals, such as: if ["$a" = = "$b"], and = equivalent
Note: The behavior of the = = function is different in [[]] and [], as follows:
1 [[$a = = z*]] # if $ A starts with "Z" (pattern match) then true
2 [[$a = = "z*"] # if $ A equals z* (character match) Then the result is true
3
4 [$a = = z*] # File globbing and word splitting will occur
5 ["$a" = = "z*"] # If a $ A equals z* (character match) Then the result is true
! = 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"] in [] structure "<" needs to be escaped.
> Greater than, in ASCII alphabetical order. such as:
if [["$a" > "$b"]
If ["$a" \> "$b"] in [] structure ">" needs to be escaped.
The-Z string is "null". The length is 0.
-N string not "null"
Examples of numerical comparisons and calculations under the shell
Comparison:
Method One: If [${a}-lt ${b}]; Then ...
This is the most basic comparison method, using LT (less than), GT (greater than), le (less than equals), GE (greater than equals), advantages: not found; disadvantage: can only compare integers, using LT,GT and other non-intuitive
Method Two: if ((${a} < ${b}) then ...
This is the Cshell style comparison, the advantage: do not use lt,gt such as difficult to remember string; disadvantage: You can only compare integers
Method Three: if (Echo ${a} ${b} | awk '! $1>$2) {Exit 1} ') then ...
This is the advantage of using awk comparison: you can compare decimals; Cons: Expressions are too complex to remember
Method Four: if (Echo ${a}-${b} | bc-q | grep-q "^-"); Then ...
This is a comparison using BC calculations, the advantage: you can compare decimals; Cons: Expressions are more complex and difficult to remember
Calculation:
Method One: typeset c=$ (expr ${a} + ${b});
Basic tools in the shell, advantages: convenient to detect whether the variable is a number; disadvantage: Only integer can be calculated, and only add and subtract can be computed, multiplication method cannot be calculated.
Method Two: Let "c=${a}+${b}"; or let "c=a+b"
Built-in command calculation, advantages: Can calculate multiplication method and bit operation; disadvantage: integer only
Method Three: Typeset c=$ ((a+b))
Cshell style calculation, advantages: Can calculate multiplication method and bit operation, introduction, easy to write; Disadvantage: decimals cannot be calculated
Method Four: typeset C=${echo ${a} ${b} | awk ' {print $1+$2} ')
With awk, the advantage: the ability to calculate decimals, can achieve a variety of computational methods, computational flexibility; Cons: Expressions are too complex
Method Five: typeset c=${echo ${a} + ${b} | bc-q)
The advantage of using awk is that you can calculate decimals, calculate more than awk, and calculate flexibly; disadvantage: the expression is too complex, the number of digits after the decimal point must be set using Scale=n, or the result may be truncated to an integer
Comparison of strings and values in the shell