Shell test command test, [], [[], shell test command test
Directory:
1.1 conditional expressions
1.2 usage of test and []
1.3 [[]
1.4 usage suggestions
Test can be used as a test expression. It supports string comparison, arithmetic comparison, file existence, attributes, and types. For example, determine whether the file is empty, whether the file exists, whether it is a directory, whether the variable is greater than 5, whether the string is equal to "longshuai", and whether the string is empty. In shell, almost all judgments are implemented using test.
[] Is equivalent to test, but it is written differently. The double brackets [[] are basically equivalent to []. It supports more conditional expressions, logical operators "&", "|", and ","! "And" () ", but these can be achieved using single brackets, but only a few more single brackets. [] In single brackets cannot implement regular expression matching, while [[] Can. Therefore, you do not need to consider using double brackets when you can use single brackets.
Both test, [], and [] use conditional expressions to complete the test. Although the use of test and [] is simple, the syntax is complicated, but the syntax of [[] is relatively simple. However, the conditional expression is interpreted first.
1.1 conditional expressions
Conditional expressions |
Meaning |
1. File detection, for example, [-e/tmp/a. log]. Unless otherwise specified, all file operations will track the soft link source file. |
-E file |
Exist) |
-F file |
Whether the file exists and is a normal file) |
-D file |
Whether the file exists and is a directory) |
-B file |
Whether the file exists and is a block device |
-C file |
Whether the file exists and is a character device |
-S file |
Whether the file exists and is a Socket file Socket |
-P file |
Whether the file exists and is named as the pipe file FIFO (pipe) |
-L file |
Whether the file exists and is a Link) |
2. File Attribute detection, such as test-r/tmp/a. log. Unless otherwise specified, all file operations will track the soft link source file. |
-R file |
Whether the file exists and the current user is readable |
-W file |
Whether the file exists and can be written by the current user |
-X file |
Whether the file exists and the current user can execute |
-U file |
Whether the file exists and SUID is set |
-G file |
Whether the file exists and the SGID is set |
-K file |
Whether the file exists and sbit (sticky bit) is set) |
-S file |
Whether the file exists and the size is greater than 0 bytes. This is used to detect whether the file is a non-blank file. |
-N file |
Whether the file exists and whether it has been modify since the last read |
3. Comparison between two files, such as test file1-nt file2 |
File1-nt file2 |
(Newer than) determine whether file1 is newer than file2 |
File1-ot file2 |
(Older than) determine whether file1 is older than file2 |
File1-ef file2 |
(Equal file) determine whether file2 and file2 are the same file, which can be used to determine the hard link. It mainly serves to determine whether both files point to the same inode in the same partition. |
4. Determine between two integers. Positive and Negative values are supported, but decimal digits are not supported. For example, test n1-eq n2 |
Int1-eq int2 |
Equal) |
Int1-ne int2 |
Not equal) |
Int1-gt int2 |
N1 is greater than n2 (greater) |
Int1-lt int2 |
N1 less than n2 (less) |
Int1-ge int2 |
N1 is greater than or equal to n2 (greater than or equal) |
Int1-le int2 |
N1 less than or equal to n2 (less than or equal) |
5. Judge the string |
-Z string |
(Zero) is the string null? True if string is a Null string |
String -N string |
Determines whether the string is not null? If the string is a Null string, false is returned. Note:-n can be omitted. |
String1 = string2 String1 = string2 |
Whether string1 and string2 are the same. Returns true if they are the same. "=" Is equivalent to "=", but "=" is more portable. |
Str1! = Str2 |
Whether str1 is not equal to str2. If not, true is returned. |
Str1> str2 |
Whether the str1 alphabetic order is greater than str2. If it is greater than, true is returned. |
Str1 <str2 |
Whether the str1 alphabetic order is smaller than str2. If it is smaller than, true is returned. |
6. logical operators, such as test-r filename-a-x filename |
-A or && |
(And) true only when both expressions are true. "-A" can only be used in test or [], & can only be used in [] |
-O or | |
(Or) If either of the two expressions is true, the value is true. "-O" can only be used in test or []. | it can only be used in [[]. |
! |
Returns the inverse of an expression. |
() |
Used to change the expression priority. |
|
1.2 usage of test and []
The usage of test and [] is very simple, but the syntax is complicated. They are tested based on the number of parameters, so the test results also depend on the number of parameters. As described below:
(1). If no parameter is included, false is directly returned.
[root@xuexi ~]# [ ];echo $?1
(2) If there is only one parameter, the test expression uses [arg]. According to the description of the conditional expression, true is returned only when arg is not empty.
[root@xuexi ~]# test haha;echo $?0
[root@xuexi ~]# test $abcd;echo $?1
[root@xuexi ~]# test '';echo $?1
(3) There are several situations when there are two parameters:
①. The first parameter is a single-object conditional operator, including file tests (such as [-e file1]), [-n string], and [-z string].
②. The first parameter is "! ", It can only be [! String], equivalent [! -N string]. Because "! "Is the inverse of the condition expression, so it is true when the string is null.
③ The first parameter is not any valid operator. An error will be reported directly.
(4) When there are three parameters, there are also several situations:
①. Binary operators are used, such as [file1-nt file2], [init1-eq int2], and [string1! = String2].
②. Logical operators are used, such as [string1-a string2] and [! -E file], [! -Z string], [! -N string].
③ If brackets are used, they can only be [(string)].
(5) When there are more than four parameters, the processing method is referred to above. For example [! String1 = string2], [string1 = string2-o string1 = string3].
Regardless of the number of parameters, it is still a test of the conditional expression, so the most important thing is the logical result of the conditional expression.
1.3 [[]
[[] Is basically equivalent to [], but some functions are more concise in writing, and [[] provides regular expression matching not available in. Therefore, the [[] function can be considered as the addition of the [] and expr commands.
Syntax format:
[[ conditional_expression ]]
Except for the following special items, other usage is equivalent to [].
(1). When the Operator Used in the conditional expression is "=" or "! = ", The right side of the operator will be matched as pattern." = "indicates that the matching is successful and 0 is returned ,"! =. However, only wildcard matching is supported, and regular expression matching is not supported. Wildcard characters include :"*","? "And" [...] ".
For example:
[root@xuexi ~]# [[ abc == a* ]];echo $?0[root@xuexi ~]# [[ abc == a*d ]];echo $?1
(2) When the Operator Used in the conditional expression is "= ~ ", The right side of the operator is matched as the pattern of the regular expression.
For example:
[root@xuexi ~]# [[ abc =~ aa* ]];echo $?0[root@xuexi ~]# [[ abc =~ aa.* ]];echo $?1
(3). Besides logical operators! And (), you can also use &, | to represent the logical and logical, Or, equivalent to [] "-a" and "-o ". However, [] does not support "-a" or "-o" anymore ".
For example:
[root@xuexi ~]# [[ 3 -eq 3 && 5 -eq 5 ]];echo $? 0
In short, [[] is required for pattern matching and regular expression matching, and [] is recommended for other purposes.
1.4 usage suggestions
We recommend that you use double quotation marks to enclose the variables and strings in [] or. For example:
name="Ma long"[ $name = "Ma long" ]
The above test statement will report an error because $ name is replaced with Ma long in the variable replacement phase, but they are not enclosed in quotation marks, so the words are split, this is equivalent to the execution of [Ma long = "Ma long"], which is obviously a wrong syntax. Therefore, double quotation marks are recommended:
[ "$name" = "Ma long" ]
In addition, there are spaces in [] and.
Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Reprinted please indicate the source: Success!