This article directory:
1.1-Bar expression
Usage notes for 1.2 test and []
1.3 [[]]
1.4 Usage Recommendations
Test can be used for testing expressions, including: string comparisons, arithmetic comparisons, file existence, attributes, types, and so on. 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 equals "Longshuai", whether the string is empty, and so on. In the shell, almost all judgments are implemented using test.
Where [] is exactly equivalent to test, it's just a different notation. The double bracket [[]] is basically equivalent to [], it supports more conditional expressions, and also allows the use of logical operators "&&", "| |", "!" in double brackets. and "()", but these use single brackets can be achieved, but write more than a few single brackets. The single-bracket [] function that cannot be implemented is a regular expression match, and [[]] can be implemented. Therefore, you do not need to consider using double-brackets in cases where you can use single-square brackets.
Test, [], and [[]] all use conditional expressions to complete the tests. Test and [] usage is simple, but syntax is more complex, but [[]] syntax is simpler. But anyway, the conditional expression is interpreted first.
1.1-Bar expression
Conditional expressions |
Significance |
1. File class detection, e.g. [-e/tmp/a.log]. If not specifically stated, all file class operations are traced to the source file of the soft link. |
-E File |
Whether the file exists (exist) |
-F File |
File exists and is normal (file) |
-D File |
Whether the file exists and is a directory |
-B File |
Whether the file exists and blocks device block |
-C file |
Whether the file exists and character device for character devices |
-S file |
If the file exists and is a socket file socket |
-P File |
File exists and is named pipe file FIFO (pipe) |
-L File |
File exists and is a linked file (link) |
2. File attribute detection, such as Test-r/tmp/a.log. If not specifically stated, all file class operations are traced to the source file of the soft link. |
-R File |
Whether the file exists and is currently readable by the user |
-W File |
Whether the file exists and the current user can write |
-X File |
Whether the file exists and the current user can execute it |
-U file |
If the file exists and suid is set |
-G file |
If the file exists and Sgid is set |
-K File |
File exists and sbit (sticky bit) is set |
-S file |
File exists and size greater than 0 bytes, which is used to detect whether the file is a non-blank file |
-N File |
Whether the file exists and has been modify since the last read |
3. Comparison between two files, such as: Test file1-nt file2 |
File1-nt File2 |
(newer than) to determine whether File1 is newer than file2 |
File1-ot File2 |
(older than) to determine whether File1 is older than file2 |
File1-ef File2 |
(equal file) to determine whether File2 and File2 are the same file, can be used in judging hard link judgment. The main significance is to determine whether two files all point to the same inode on the same partition |
4. The decision between two integers supports positive negative numbers, but does not support decimals. For example, test n1-eq n2 |
Int1-eq Int2 |
Two values equal (equal) |
Int1-ne Int2 |
Two values unequal (not equal) |
INT1-GT Int2 |
N1 greater than N2 (greater than) |
Int1-lt Int2 |
N1 smaller than N2 (less than) |
Int1-ge Int2 |
N1 greater than or equal to N2 (greater than or equal) |
Int1-le Int2 |
N1 less than or equal to N2 (lesser than or equal) |
5. Decision String |
-Z String |
(zero) determines if the string is empty? True if string is an empty string |
String -N String |
Determine if the string is not empty? False if string is an empty string. Note:-N can be omitted |
string1 = string2 string1 = = string2 |
String1 and string2 are the same. The same returns true. "=" and "=" are equivalent, but "=" portability is better |
Str1! = str2 |
STR1 is not equal to str2, if unequal, returns true |
str1 > STR2 |
STR1 If the alphabetic order is greater than str2, or true if it is greater than |
STR1 < STR2 |
STR1 If the alphabetic order is less than str2, or true if it is less than |
6. Logical operators, for example: Test-r filename-a-x filename |
-A or && |
True if both expressions are true. "-a" can only be used in test or [],&& can only be used in [[]] |
-O or | | |
(or) either of the two expressions is true. "-O" can only be used in test or [], | | Can only be used in [[]] |
! |
To reverse an expression |
( ) |
Used to change the priority of an expression |
|
Usage notes for 1.2 test and []
The use of test and [] is very simple, but the syntax is more complex. They are tested according to the number of parameters, so the result of the test depends on the number of parameters. As described below:
(1). Returns false directly without any parameters.
[[Email protected] ~]# []; Echo $? 1
(2). When there is only one argument, the test expression takes [arg], and, depending on the description of the conditional expression, returns true only if ARG is non-null.
[Email protected] ~]# test haha; Echo $? 0
[Email protected] ~]# test $ABCD; Echo $? 1
"; Echo $? 1
(3). Two parameters, there are several cases:
①. The first parameter is the Monocular condition operator, including the file class test (such as [-e File1]) and [-N string], [-Z string].
②. The first parameter is "!" , you can only be [! string], which is equivalent to [!-n string]. Because "!" is to reverse the conditional expression, so true if the string is empty.
③ The first parameter is not a valid operator. Will directly error
(4). Three parameters, there are several cases:
①. Binocular operators are used, such as [File1-nt file2],[Init1-eq Int2] and [string1! = string2].
②. Logical operators are used, such as [String1-a string2], [!-e file], [!-Z string], [!-n string].
③. Parentheses are used, only [(string)].
(5). Four parameters above, then the processing method reference above. such as [! string1 = = string2], [string1 = = String2-o string1 = = String3].
Regardless of the number of parameters, in general, the conditional expression is tested, 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 and [[]] provide regular expression matching that [] does not have. Therefore, the function of [[]] can be considered as the addition of the [] and expr commands .
Syntax format:
[[Conditional_expression]]
In addition to the following special notes, the remaining usage is equivalent to [].
(1). When the operator used in a conditional expression is "= =" or "! =", the right side of the operator is matched as pattern, and "= =" indicates that the match succeeds then returns 0, "! =" instead. However, this is only a wildcard match, and regular expression matching is not supported. Wildcard characters include: "*", "?" and "[...]".
For example:
[[Email protected] ~]# [[ABC = = *]]; Echo $? 0 ~]# [[ABC = = A*d]]; Echo $? 1
(2). When the operator used in a conditional expression is "=~", the right side of the operator is matched as the pattern of the regular expression.
For example:
[[Email protected] ~]# [[ABC =~ aa*]; Echo $? 0 ~]# [[ABC =~ aa.*]; Echo $? 1
(3). In addition to using logical operators! and (), you can also use &&, | |, respectively, to represent logical and logical OR, equivalent to "-a" and "-O" for []. However, [[]] no longer supports "-a" and "-o".
For example:
3 3 5 5 ]]; Echo 0
In summary, you need to use [[]] In addition to pattern matching and regular expression matching, and [] are recommended for the rest of the time.
1.4 Usage Recommendations
whether [] or [[]], it is recommended to enclose the inside variable, the string using double quotation marks. For example:
Name="ma long""ma long" ]
The above test statement will give an error, because in the variable substitution phase, $name are replaced with Ma long, but they are not in quotation marks, so the word split, which is equivalent to the implementation of [Ma long = "Ma long"], obviously this is the wrong syntax. Therefore, it is recommended to add double quotes:
" $name " " Ma Long " ]
In addition, each place has spaces in [] and [[]].
Back to series article outline:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Reprint Please specify source:http://www.cnblogs.com/f-ck-need-u/p/7427357.html Note: If you think this article is not bad please click on the lower right corner of the recommendation, with your support to inspire the author more enthusiasm for writing, thank you very much!
Shell Test command test, [], [[]]