Shell string Comparisons
The method of comparison of integers in a Shell and the method of comparing strings, such as equals, not equal to, greater than, greater than or equal, less than, etc.
Binary comparison operator, comparing variables or comparing numbers. Note the difference between numbers and strings.
--------------------------------------------------------------------------------
Integer comparisons
-eq equals, such as: if ["$a"-eq "$b"]
-ne is not equal to, such as: if ["$a"-ne "$b"]
-GT is greater than, such as: if ["$a"-gt "$b"]
-ge is greater than or equal to, such as: if ["$a"-ge "$b"]
-lt is less than, such as: if ["$a"-lt "$b"]
-le is less than or equal, such as: if ["$a"-le "$b"]
< less than (requires double brackets), such as: (("$a" < "$b")
<= is less than or equal (requires double parenthesis), such as: (("$a" <= "$b")
> Greater than (requires double brackets), such as: (("$a" > "$b")
>= is greater than or equal (requires double parenthesis), such as: (("$a" >= "$b")
Note: [] the contents of [] must be separated from the [] space, otherwise there will be an error. --------------------------------------------------------------------------------
String comparisons
= equals, such as: if ["$a" = "$b"]
= = equals, such as: if ["$a" = = "$b"], and = equivalent
Note: the function of = = = is different in [[]] and [], as follows:
1 [[$a = = z*]] # If $a starts with "Z" (pattern match) then it will be 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 equals z* (character match), then the result is true
A little explanation, about the file globbing is a shorthand for documents, such as "*.c" is, and so on.
But file globbing is not a strict regular expression, although in most cases the structure is more like.
!= is not equal to, such as: if ["$a"!= "$b"]
This operator will use pattern matching in the [[]] structure.
< less than, in ASCII alphabetical order. For example:
if [["$a" < "$b"]]
If ["$a" \< "$b"]
Note: In the [] structure, "<" needs to be escaped.
> Greater than, in ASCII alphabetical order. For example:
if [["$a" > "$b"]]
If ["$a" \> "$b"]
Note: In the [] structure, ">" needs to be escaped.
Refer to Example 26-11 to see examples of this operator application.
The-Z string is "null". That is, the length is 0.
-N string is not "null"
Attention:
Using-n tests in the [] structure must be caused by the variable "". Use a string that is not ""! -Z
Or the string itself, which is not quoted, is placed in the [] structure. Although in general you can
To work, but it's not safe. It is a good habit to use "" to test strings.
--------------------------------------------------------------------------------
Shell Number Comparison Example
Method One: If [${a}-lt ${b}]; Then ...
This is the most basic comparison method, using LT (less than), GT (greater than), LE (less than equal), GE (greater than equal), advantages: not found; disadvantage: can only compare integers, use lt,gt and so on not intuitive
Method Two: if ((${a} < ${b})) then ...
This is the Cshell style comparison, advantages: Do not use LT,GT and other difficult to remember the string; disadvantage: or only to compare integers
Method Three: if (Echo ${a} ${b} | awk '! $1>$2) {Exit 1} ') then ...
This is the advantage of using awk: you can compare decimals; disadvantages: Expressions are too complex to remember
Method IV: if (echo ${a}-${b} | bc-q | grep-q "^-"); Then ...
This is calculated using the BC comparison, the advantages: you can compare decimals; disadvantages: more complex expressions, difficult to remember
--------------------------------------------------------------------------------
Shell calculations
Method One: typeset c=$ (expr ${a} + ${b}); /typeset c= Expr ${a} + ${b};
The basic tools in the shell, advantages: convenient to detect whether the variable is a number; disadvantages: only integers can be computed, and only addition and subtraction can be computed, and the multiplication and division method cannot be computed.
Method Two: Let "c=${a}+${b}"; or let "c=a+b"
Built-in command calculation, advantages: can calculate multiplication and division and bitwise operation; disadvantages: only integers can be computed
Method III: Typeset c=$ ((a+b))
Cshell style calculation, advantages: can calculate multiplication and division method and bit operation, introduction, easy to write; disadvantage: cannot calculate decimal
Method Four: typeset C=${echo ${a} ${b} | awk ' {print $1+$2} ')
The advantage of using awk calculation is that it is possible to compute decimal numbers, to achieve a variety of calculations, and to compute flexibly; Disadvantages: too complex an expression
Method Five: typeset c=${echo ${a} + ${b} | bc-q)
The advantage of using awk calculation is that you can calculate decimals, compute more than awk, and compute 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
--------------------------------------------------------------------------------
awk ' {print $} ' class.txt | grep ' ^[0-9.] ' > Res
–b returns True when file exists and is a block file
-C Returns True when file exists and is a character file
-D returns True when pathname exists and is a directory
-E Returns True when pathname specified file or directory exists
-F Returns True when file exists and is a regular file
-G returns True when the file or directory specified by pathname exists and the Sgid bit is set
-H Returns True when file exists and is a symbolic link file, which is not valid on some old systems
-K returns True when a file or directory specified by pathname exists and a "sticky" bit is set
-P Returns True when file exists and is a command pipe
-R when the file or directory specified by pathname is present and returned as true when it is readable
-S returns True when file size is greater than 0 o'clock
-U returns True when the file or directory specified by pathname exists and the SUID bit is set
-W Returns True when the file or directory specified by pathname exists and is executable. A directory that is accessed for its content is necessarily executable.
-O Returns True when the file or directory specified by pathname exists and the user specified by the valid user ID of the current process
--This article from: http://linglong2110.iteye.com/blog/1058021