Shell logical operators involve the following types. Therefore, you only need to make proper selection to solve many complicated judgments and get twice the result with half the effort.
I. logical operators
Logical volume label |
Meaning |
1. |
The detection logic volume mark for files and directories! |
-F |
Common! Check whether "file" exists. For example: if [-f filename] |
-D |
Common! Check whether the "directory" exists |
-B |
Detects whether a block file is used 』 |
-C |
Detects whether it is a "character file 』 |
-S |
Detects whether a "socket Tag file" is used 』 |
-L |
Detect whether it is a "Symbolic Link file 』 |
-E |
Detects whether something exists! |
2. |
AboutProgramLogical volume label! |
-G |
Checks whether the program is owned by the GID. |
-O |
Checks whether the programs executed by the UID own |
-P |
Detect whether it is the name pipe or FIFO of information transmitted between programs (to be honest, this is hard to understand !) |
3. |
File Attribute Detection! |
-R |
Whether it is a readable attribute |
-W |
Detects whether a property can be written. |
-X |
Whether it is an executable attribute |
-S |
Whether to detect non-empty files 』 |
-U |
Check whether the property of "SUID" exists |
-G |
Checks whether a property with "SGID" exists. |
-K |
Checks whether the property of "Sticky Bit" exists. |
4. |
Judgment and comparison between two ArchivesFor example, [test file1-nt file2] |
-NT |
The first archive is newer than the second archive. |
-Ot |
The first archive is older than the second archive. |
-Ef |
The first and second archives are the same (links and other archives) |
5. |
Logical "and" or )』 |
&& |
Logical and |
| |
Logical or |
Operator number |
Meaning |
= |
Equal to: integer or string comparisonIf it is in [], it can only be a string |
! = |
Not equal to: integer or string comparisonIf it is in [], it can only be a string |
< |
Less than applied to: integer comparisonThe string cannot be used in []. |
> |
Greater than applied to: integer comparisonCannot be used in []String |
-EQ |
Equal to: integer comparison |
-Ne |
Not equal to: integer comparison |
-Lt |
Less than applied to: integer comparison |
-GT |
Greater than applied to: integer comparison |
-Le |
Less than or equal to: integer comparison |
-Ge |
Value greater than or equal to integer comparison |
- |
Both parties establish (and) logical expression-a logical expression |
-O |
Unilateral or logical expression-O logical expression |
-Z |
Null String |
-N |
Non-Null String |
Ii. logical expressions
Usage:Test expression
For example:
[Root @ localhost ~] # Test 1 = 1 & Echo 'OK'
OK
[Root @ localhost ~] # Test-D/etc/& Echo 'OK'
OK
[Root @ localhost ~] # Test 1-EQ 1 & Echo 'OK'
OK
[Root @ localhost ~] # If test 1 = 1; Then ECHO 'OK'; FI
OK
Note: All characters and logical operators are separated by spaces and cannot be connected together.
[Root @ localhost ~] # [1-EQ 1] & Echo 'OK'
OK
[Root @ localhost ~] #[2 <1] & Echo 'OK'
-Bash: 2: no such file or directory
[Root @ localhost ~] #[2 \ <1] & Echo 'OK'
[Root @ localhost ~] # [2-GT 1-A 3-lt 4] & Echo 'OK'
OK
[Root @ localhost ~] # [2-GT 1 & 3-lt 4] & Echo 'OK'
-Bash: [: Missing ']'
Note: In [] expressions, common >,< escape characters must be added to indicate that the string size is compared, and the acill code position is used as the comparison. The <> operator and logical operator are not directly supported | & it must be represented by-A [and]-O [or ].
[Root @ localhost ~] # [1-EQ 1] & Echo 'OK'
OK
[Root @ localhost ~] $[[2 <3] & Echo 'OK'
OK
[Root @ localhost ~] $[[2 <3 & 4> 5] & Echo 'OK'
OK
Note: The [[] operator is only an extension of the [] operator. Supported <,> escape characters are not required for symbol operations. The escape characters are compared by strings. Which supports logical operators: || &&
Iii. Performance Comparison
Bash conditional expressions have three almost equivalent symbols and commands: Test, [] and [[]. Generally, we are used to the form of if []; then. [[], According to ABS, is intended to be compatible with operators such as <. The following is a comparison of their performance, and found that [[] is the fastest.
$ Time (for m in {1 .. 100000}; do test-D.; done ;)
Real 0m0. 658 s
User 0m0. 558 s
Sys 0m0. 100 s
$ Time (for m in {1 .. 100000}; do [-D.]; done ;)
Real 0m0. 609 s
User 0m0. 524 S
Sys 0m0. 085 s
$ Time (for m in {1 .. 100000}; do [[-D.]; done ;)
Real 0m0. 311 s
User 0m0. 275 s
Sys 0m0. 036 s
[[] is highly compatible without considering its compatibility with Bash and SH in earlier versions, in addition, the performance is relatively fast. You can use this operator when performing conditional operations.