This article mainly introduces the shell commonly used operator Summary, this article explained the arithmetic operator, the relational operator, the test operator and so on content, needs the friend to be possible to refer to under
First, the arithmetic operation
Copy code code as follows:
+ - * / %
Second, the relationship operation
Copy code code as follows:
< > >= <= = =!= && | |
Third, the test operation
Testing command: Test [] [[]] [()]
1. test file status
The code is as follows:
-D Directory
-B Block Equipment
-S file length > 0, Non-empty
-F Regular Documents
-W can write
-L Symbolic connection
-u file has suid bit settings
-R Readable
-X Executable
such as: [-w tmp.txt] test file tmp.txt is writable
2. String test
The code is as follows:
= Two strings equal
!= Two strings Not equal
-Z Empty string
-N Non-empty string
$ var1= ' abc '
$ Var2= ' 123 '
$ ["$VAR 1" = "$VAR 2"]
$ echo $?
1
3. Test values
The code is as follows:
-eq equals
-ne is not equal to
-GT Greater than
-lt less than
-ge is greater than or equal to
-le less than or equal
4. Expand test symbols [[]] (())
The code is as follows:
# [[2 > 10]] # result error
# echo $?
0
# ((2 >10)) The result is correct
# echo $?
0
# [[[' AA ' = ' AA ']]
# echo $?
0
# ("AA" = "AA") #结果错误
# echo $?
1
Iv. Conclusion
Compare numbers, use (())
Other tests use [[]]
Mixed tests that contain numeric comparisons, using standard syntax