shell script to sort out the basics <two>: test operators

First, the integer symbol size comparison test; note: the operator must stay on both sides of a space

[Intnum1 -eq intnum2] or [[intnum1 -eq intnum2]]
-eq: equal
[Intnum1 -ne intnum2] or [[intnum1 -ne intnum2]] -ne: no equal
[Intnum1 -gt intnum2] or [[intnum1 -gt intnum2]] -gt: greater than
[Intnum1 -ge intnum2] or [[intnum1 -ge intnum2]] -ge: greater than or equal
[Intnum1 -en intnum2] 或是 [[intnum1 -en intnum2]] -lt: less than
[Intnum1 -le intnum2] or [[intnum1 -le intnum2]] -le: less
((intnum1 == intnum2)) The spaces may be omitted Returns true number is equal to two
((Intnum1! = Intnum2)) Two numbers are not equal returns true
((intnum1 > intnum2)) Returns true number is greater than two
((intnum1 >= intnum2)) Greater than or equal to two returns true
((Intnum1 <intnum2)) Returns true in a number less than two
((Intnum1 <= intnum2)) Number is less than or equal to two returns true

Second, these operations are consistent with the test file for [[]], [], test almost universal, more man test

 -f file 

File exists and is a regular file is true that the test expression into

-d file (directory)

File exists and is a directory file is true that the test expression is established

-s file

File exists and is established as true size is not 0, that is, the test expression

-e file

File exists is true that the test expression is established, as long as the file on the line, to distinguish between the "-f"

-r file File exists and is readable true that the test expression is established
-w file

File exists and is writable true that the test expression is established

-x file File exists and is executable true that the test expression is established
-L file

File exists and is a linked file is true that the test expression is established

Third, the character string tests

-z "string"  If the string length is 0 true; [-z "abc"] && echo 1 || echo 0

- n "string"

If the string length is not 0 is true

"String 1" = "String 2"

Two strings are equal to true

"String 1"! = "String 2"

The two strings are true

note

String containing double quotation marks; Comparative character spaces at both ends

Fourth, the logical connectors

In [] and the Test                                     (()) and [[]] than in the                                   described


  -a                                                             &&                                             and the two are true is true


  -o                                                               ||                                                 or or both ends there is a true true


 !                                                                 !                                                   Not non-contrary is true


Guess you like

Origin blog.51cto.com/14294148/2434430