The Shell is basically a command interpreter, similar to a DOS command. It receives user commands (such as LS, and so on), and then invokes the appropriate application. The more common shell has the standard Bourne shell (SH) and C shell (CSH).
Many times when writing a Shell script is easy to forget some of the logical operators and expressions, so the excerpt below, left for a rainy day query use.
Shell logical operators are involved in several types, as long as the appropriate choice, can solve many of our complex judgments, to achieve a multiplier effect.
I. Logical judgment
1. On the logical judgment of documents and directories
-F commonly used. Determine if "file" is a normal file, such as: if [-f filename]
-D used. Determine if "directory" exists
-B To determine if it is a "block file"
-C to determine if it is a "character file"
-S to determine if it is a "socket tag file"
-L to determine if it is a "symbolic link file"
-E to determine if "file" exists
2, on the logic of the process of judgment
-G Determines whether the program performed by the GID has
-O Determines whether the program executed by the UID has
-P Determines whether the name pipe or FIFO that transmits information between programs
3, about the document property judgment
-R to determine if the property is readable
-W to determine if it is a property that can be written
-X determines whether the property is executable
-S to determine if it is "not a blank file"
-U to determine if there is a "SUID" property
-G to determine if there is a "SGID" property
-K to determine if there is a "sticky bit" property
4, two documents between the judgment and comparison
-nt the first file is newer than the second file
-ot The first file is older than the second file
-ef The first file is the same file as the second file (a file like link)
5. Boolean operators
&& Logic and
|| Logical OR
! Logical non
Second, operational symbols
= equals apply to: integer or string comparison if in [], it can only be a string
!= is not equal to apply to: integer or string comparisons if in [], only strings
< less applied to: integer comparison in [], cannot use the presentation string
> Greater than Applied: integer comparison in [], cannot use the presentation string
-eq equals apply to: integer comparison
-ne is not equal to apply to: integral type comparison
-LT is less than applied to: integral type comparison
-GT is greater than applies to: integer comparison
-le less than or equal to: integer comparison
-ge is greater than or equal to: integer comparison
Both are established (and) equal to &&
-O Unilateral establishment (or) equivalent to | |
-Z Empty string
-N Non-empty string
Three, logical expression
Note: All characters and logical operators are separated directly by "spaces" and cannot be joined together.
1. Test command
How to use: Test EXPRESSION
Such as:
The code is as follows |
Copy Code |
[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 |
2. Compact expression
[] An expression
The code is as follows |
Copy Code |
[Root@localhost ~]# [1-eq 1] && echo ' OK ' Ok [Root@localhost ~]# [2 < 1] && echo ' OK ' -bash:1: 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 >,< needs to be preceded by an escape character, which indicates a string size comparison and a Acill code position. <> operator not directly supported, and logical operators | | and && it needs to be represented by-a[and]–o[or].
[[]] An expression
The code is as follows |
Copy Code |
[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 just an extension of the [] operator. The ability to support <,> symbolic operations does not require an escape character, it is also a string comparison size. Inside supports logical operators | | and &&
Bash has three almost equivalent symbols and commands in the conditional expression: test,[] and [[]]. Usually, it is customary to use the form of if [];then. and [[]] the appearance, according to ABS, is to be compatible with the operators such as ><.
Without considering the compatibility of low version bash and SH, using [[]] is strong compatibility and performance is faster, you can use this operator when doing conditional operations.