by Francis_hao Oct 1,2017
This article extracts the Man Handbook from bash
Conditional expressions
The conditional expression is composed of a composite command [[or] built-in command test and [used for testing file properties and string or numeric comparisons. The expression form is shown in the following table, and if the file form is/dev/fd/n, then the document descriptor Nwill be tested, if it is/dev/stdin,/dev/stdout, or/dev/stderr. Then the file descriptor 0,1 and 2 will be tested. Unless otherwise specified, testing a connection is a test of the file it points to, not the connection itself.
An expression |
Description |
File Properties Test |
-A file |
True if file exists |
-B File |
True if file exists and is block |
-C file |
True if file exists and is character files |
-D File |
True if file exists and is a directory |
-E File |
True if file exists |
-F File |
True if file exists and is normal |
-G file |
If file exists and the set setting-group ID is true, see Section 4.4, 12.3 of the reference "1" |
-H file |
True if file exists and is a symbolic connection |
-K File |
If file exists and its sticky position is true, refer to man chmod |
-P File |
True if file exists and is a command pipeline (FIFO) |
-R File |
True if file exists and is readable |
-S file |
True if file exists and its length is greater than 0 |
-T FD |
True if the file descriptor fd is open and points to a terminal |
-U file |
If file exists and is set-user-id is true, see Section 4.4 of Reference "1" |
-W File |
True if file exists and is writable |
-X File |
If file exists and is executable |
-G file |
True if file exists and is owned by a valid group ID, see Reference "1" section 4.4 |
-L File |
True if file exists and is a symbolic connection |
-N File |
True if file exists and has been modified since the last read (modified) |
-O File |
True if file exists and is owned by a valid user ID, see section 4.4, "1" |
-S file |
True if file exists and is a socket |
File1-ef File2 |
True if File1 and file2 are pointing to the same device's inode |
File1-nt File2 |
If file1 is newer than file (modified), or file1 exists file2 does not exist in the true |
File1-ot File2 |
If File1 is older than file (modified), or file1 exists file2 does not exist in the true |
Shell-related |
-O optname |
If the shell option Optname is true, refer to the-O option for the built-in instruction set |
-V varname |
True if the shell variable varname is set |
String test |
-Z String |
True if string length is 0 |
String -N String |
True if string length is not 0 |
string1 = = string2 string1 = string2 |
True if string1 and string2 are equal, = should only be used by test |
String1! = string2 |
True if the strings are not equal |
String1 < string2 |
True if the dictionary order string1 before string2 |
String1 > string2 |
True if the dictionary order string1 after string2 |
Numerical test |
Arg1 OP arg2 |
OP is one of-eq,-ne,-lt,-le,-GT, or-ge, and the value can be negative. |
Compound command [[
[[ expression ]]
returns 0 or 1 based on the result of the conditional expression expressions. in [[and]], Word segmentation and path expansion are not performed, other extensions are executed, and conditional operators, like-F, must be non-referenced to be recognized as operators. The < and > operators use the current locale for dictionary sorting.
When using the = = and! = operators, the operands on the right can be a pattern (pattern Matching), and if you want to match the pattern itself, use quotation marks.
Another available two-dollar operand, =~, has the same precedence as = = and! =. When it is used, the operand on the right is considered an extended regular expression (see reference "2"), and if the option to enable Shell is nocasematch, then the expression is not case sensitive.
Expression can be combined using the following operators, with precedence ordered from high to low
Operator |
Description |
(expression) |
Used to change the priority level |
! Expression |
The result of expression is reversed |
Expression1 && expression2 |
Logic and, if expression1 enough to show the true and false of the whole expression, then expression2 will not be evaluated |
expression1 | | Expression2 |
Logical OR, if expression1 is sufficient to indicate the true and false of the entire expression, then expression2 will not be evaluated |
Built-in command test and [
Test Expr
[ expr ]
Depending on the result of the conditional expression expr returns 0 or 1, each operator and operand must be a separate parameter, and test does not accept any options, even the end of the argument--or not.
The expression can be combined by the following operators, with precedence ordered from high to low
Operator |
Description |
! Expr |
The result of expr is reversed |
(expr) |
Used to change the priority level |
Expr1-a EXPR2 |
Logic and |
Expr1-o EXPR2 |
Logical OR |
The < and > operators use ASCII for dictionary sorting.
This article was authored by Liu Yinghao and licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 Chinese mainland license agreement in the same way. Welcome reprint, please specify the source:
Reprinted from: http://www.cnblogs.com/yinghao1991/p/7617965.html
Reference
"1" W.richard Stevens, Stephen a.rago Hu Zhengwei Zhangya Yingyu translation "Advanced Programming of UNIX Environment (3rd edition)" Beijing: People's post and post publishing house 2014.6
"2" blog Park http://www.cnblogs.com/yinghao1991/p/7617004.html posix.2 Regular expression
"3" Man sh
Conditional expressions in the shell