Condition test: Recommended format 2
Format 1:test< Test expression > man test See Related Help
Format 2: [< test expression;]
Format 3: [[< test expression;]]
Description: A wildcard character can be used for pattern matching in [[]] && | | > < operators can be applied to [[]], but not in []
[[email protected] ~]# test-f file && echo 1 | | Echo 0
0
[[email protected] ~]# Touch file
[[email protected] ~]# test-f file && echo 1 | | Echo 0
1
[Email protected] ~]# test! -F file && echo 1 | | Echo 0
0
[Email protected] ~]# arg= "Martin"
[Email protected] ~]# test-z "$arg" && echo 1 | | Echo 0
0
[[Email protected] tools]# [-F file]&& echo 1 | | Echo 0
0
[[Email protected] tools]# [[-F File &&-D dir]]&& echo 1 | | Echo 0
0
File Test Operators
-F File
-D Dirrctory
-S size file exists and is not empty is true
-e exist can be a file or a directory
-R Read
-W Write
-X Excute.
-L Link
F1-nt F2 Newer than if file F1 than file F2 new is True
F1-ot F2 older than
[Email protected] tools]# mkdir Martin
[[Email protected] tools]# [-F Martin]&& echo 1 | | Echo 0
0
[[Email protected] tools]# [-D Martin]&& echo 1 | | Echo 0
1
[Email protected] tools]# [-e Martin]&& echo 1 | | Echo 0
1
Common string test operators
-Z "string" if the length is 0 is true, Z can be understood as zero
-N "string" if the length is not 0 is true, n can be understood as no zero
"String 1" = "String 2" if string 1 equals string 2 is true, you can use = = instead of =
"String 1"! = "String 2" if string 1 is not string 2 is true, but cannot use!== instead of! =
string or string variable comparisons are double-quoted and then compared
string comparison, compare symbols at both ends preferably with a space, multi-reference system script
[Email protected] tools]# sed-n ' 30,31p '/etc/init.d/network
# Check that networking are up.
["${networking}" = "no"] && Exit 6
[Email protected] tools]# martin= "Hello"
[[Email protected] tools]# ["${martin}" = "Hello"]&& echo 1 | | Echo 0
1
[[Email protected] tools]# ["${martin}" = "hello123"]&& echo 1 | | Echo 0
0
[[Email protected] tools]# [-N "abc"]&& echo 1 | | Echo 0
1
[[Email protected] tools]# [-N ""]&& echo 1 | | Echo 0
0
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/89/24/wKiom1gIumHgEW9nAAA_xDwA2ao164.png "title=" 1.png " alt= "Wkiom1giumhgew9naaa_xdwa2ao164.png"/>
[[Email protected] tools]# [12-eq]&&echo 1 | | Echo 0
0
[[Email protected] tools]# [12-lt]&&echo 1 | | Echo 0
1
[[Email protected] tools]# [12-gt]&&echo 1 | | Echo 0
0
[[Email protected] tools]# [[>]]&&echo 1 | | Echo 0
0
[[Email protected] tools]# [[<]]&&echo 1 | | Echo 0
1
logical operators
The logical operator used in [] in [] is explained
-A && and
- O | | or
! ! Not take counter
[[Email protected] tools]# [-F "$f 1"-a-f "$f 2"]&& echo 1 | | Echo 0
1
Observing System Script NFS
# Source Networking configuration.
[-f/etc/sysconfig/network] &&. /etc/sysconfig/network
[Email protected] scripts]# sed-n ' 87,90p '/etc/init.d/nfs
["$NFSD _module"! = "Noload"-a-x/sbin/modprobe] && {
/sbin/modprobe NFSD
[-N "$RDMA _port"] &&/sbin/modprobe SVCRDMA
}
This article from "Thick tak" blog, declined reprint!
Shell Condition Test Summary