When a conditional test expression is executed, it usually returns TRUE or false, as if the return value after executing the command is 0 for true, and not 0 for false. In bash programming, the usual syntax for conditional testing is as follows:
650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/23/a52c3b6a03e527ebd588aab2aec475e2.png "title=" screen shot 2017-10-23 pm 10.05.48.png "width=" "height=" 204 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:700PX;HEIGHT:204PX; " alt= "A52c3b6a03e527ebd588aab2aec475e2.png"/>
Description
(1) Syntax 1 is equivalent to Syntax 2, and Syntax 3 is an extension of Syntax 1;
(2) Syntax 4 is often used for computation;
(3) in [[]] (double brackets) can use wildcard characters and so on pattern matching, this is the difference between several other grammatical formats;
(4) &&,| |,>,< and other operators can be applied to [[]], in [] generally with-a,-o,-gt (for integers),-lt (for integers),! = instead of the above operator;
(5) For a relational operation of integers, you can also use the Shell's Arithmetic operator (()).
Recommendation: Work with Syntax 2!
Commonly used file test operators
650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/23/8eb7c4c18005aa24ee7ec2f2a3675152.png "title=" screen shot 2017-10-23 pm 10.26.46.png "width=" "height=" 361 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:361px; " alt= "8eb7c4c18005aa24ee7ec2f2a3675152.png"/>
Give a special example:
[email protected] ~]# touch WTF
[email protected] ~]# ll WTF
-rw-r--r--1 root root 0 October 03:25 wtf
[Email protected] ~]# chmod 001 WTF
[email protected] ~]# ll WTF
---------x 1 root root 0 October 03:25 wtf
[[Email protected] ~]# [-R WTF] && echo 1 | | Echo 0
1
[[Email protected] ~]# [-w WTF] && echo 1 | | Echo 0
1
[[Email protected] ~]# [-x WTF] && echo 1 | | Echo 0
1
Description: WTF This ordinary file does not have readable and writable permissions, why does it return 1? This is because we are using the root account for the reason! Therefore, we are testing a file read, write, execute and other properties, not only according to the file attributes rwx identification to judge, but also to see whether the current execution of the test user can actually follow the corresponding permissions to manipulate the file.
Special wording and problems of variables
[Email protected] ~]# echo $WTF
[Email protected] ~]# [-e $WTF] && echo 1 | | Echo 0
1
[Email protected] ~]# [-E "$WTF"] && echo 1 | | Echo 0
0
Note: The variable WTF does not exist, but when the [-e $WTF] is directly judged, the return value is 1, which is inconsistent with logic, so this time the variable WTF to add double quotation marks "$WTF".
The file is the same as the unquoted structure, such as:
[Email protected] ~]# cd/tmp
[[email protected] tmp]# ls
TMP0F7ZGN tmpcywyqy tmpEAwY35 Tmpinhrdn tmpwk_xzo yum.log
[[Email protected] tmp]# [-e/tmp/wutengfei] && echo 1 | | Echo 0
0
[Email protected] tmp]# [-E "/tmp/wutengfei"] && echo 1 | | Echo 0
0
This article is from the "Hand of the Paladin Control" blog, please make sure to keep this source http://wutengfei.blog.51cto.com/10942117/1975410