File test expression
File usage test expression
Before explaining expression test file, first give an example of life: If you're looking for old boy teacher billiards, you must not go to the pool hall, but will first call me, there's no time to play with the next question, Similarly, we need to deal with when programming an object, the object needs to be tested, only to meet the requirements, take action treatment, the benefits of doing so is to avoid procedural errors and unnecessary consumption of system resources, this should be the test object can be a file, strings, numbers and the like.
When a file writing test expression, generally used in the following table file test operators
Common file test operators
Common file test operators | Explanation |
-f file | If the file exists and is a regular file true |
-d file | If the file exists and is a directory file is true |
-s file | If the file exists and is not empty (the file size is non-zero) is true |
-e file | If the file exists really, to distinguish between -f |
-r file | -r file if the file exists and is readable true |
-w file | If the file exists and is writable true |
-x file | If the file exists and is executable true |
-L file | If the file exists and is a linked file is true |
f1 -nt f2 | If the file f1 is newer than the file f2 true |
f1 f2 -ot | If the file f1 is older than the file f2 true |
Special note: these operators for [[]], [], Test nearly universal more query operators make man test
For example file test expression
Trivial File test expression, for example
regular file (test file types)
[root@node1 script]# touch oldboy [root@node1 script]# ls -l oldboy -rw-r--r-- 1 root root 0 Aug 29 07:44 oldboy [root@node1 script]# [ -f oldboy ] && echo 1 || echo 0 1
Catalog file (test file types)
[root@node1 script]# mkdir oldgirl [root@node1 script]# [ -f oldgirl ] && echo 1 || echo 0 0 [root@node1 script]# [ -d oldgirl ] && echo 1 || echo 0 1 [root@node1 script]# [ -e oldgirl ] && echo 1 || echo 0 1 [root@node1 script]# [ -f oldboy ] && echo 1 || echo 0 1
Test file attributes, for example:
[root@node1 script]# ls -l oldboy -rw-r--r-- 1 root root 0 Aug 29 07:44 oldboy [root@node1 script]# [ -r oldboy ] && echo 1 || echo 0 1 [root@node1 script]# [ -w oldboy ] && echo 1 || echo 0 1 [root@node1 script]# [ -x oldboy ] && echo 1 || echo 0 0 [root@node1 script]# chmod +x oldboy [root@node1 script]#[-X Oldboy] && echo echo 1 || 0 1 [root @ node1 Script] # chmod 000 Oldboy [root @ node1 Script] # LS the -l Oldboy ---------- 1 root root 0 Aug 29 07 : 44 Oldboy [root @ node1 Script] # [-r Oldboy] && echo # 1 || echo 0 because the user is larger root, root privileges, regardless of use to root for 1
Test shell variables For example:
First we define two variables file1 and file2, and were given two system files path and file name of values
[the root @ node1 Script] # file1 = / etc / Services; file2 = / etc / semicolon is used to separate the rc.local # → two commands [the root @ node1 Script] # echo $ file1 file2 $ / etc / Services / etc / rc. local
Example 1: Test on a single file variable
[the root @ node1 Script] # [$ -f file1] && echo echo. 1 || # 0 → file exists and it is true for the general file (. 1) . 1 [the root @ node1 Script] # [-d $ file1] && echo 1 || echo 0 # → file is not a directory it is false (0) 0 [root @ node1 Script] # [-f "$ file1"] && echo 1 || echo 0 # → file exists and the size is not 0, so true (1) 1 [root @ node1 Script] # [-f "$ file"] && echo # 1 → 0 || echo file exists it is true (1) 0
Example 2: Test on a single directory variables
[root@node1 script]# dir1=/etc/ [root@node1 script]# [ -e "$dir1" ] && echo 1 || echo 0 1 [root@node1 script]# [ -w /etc/services ] && echo 1 || echo 0 1 [root@node1 script]# su - chenxi Last login: Thu Aug 29 07:39:00 CST 2019 on pts/0 [chenxi@node1 ~]$ [ -w /etc/services ] && echo 1 || echo 0 0
Special example: If the variable without double quotes. The test results may be incorrect:
[root @ node1 Script] # echo $ file7 [root @ node1 Script] # [-f "$ file7"] && echo echo || 0 1 0 [root @ node1 Script] # [-f $ file7] && echo 1 | | echo 0 1 # → obviously does not exist yet returned $ file7 1
Example 3: the variable content into physical file path testing
[root@node1 script]# [ -f /etc/service ] && echo 1 || echo 0 0 [root@node1 script]# [ -f /etc/services ] && echo 1 || echo 0 1 [root@node1 script]# [ -f "/etc/service" ] && echo 1 || echo 0 0 [root@node1 script]# [ -f "/etc/services" ] && echo 1 || echo 0 1
Tip: If the entity is a file path in quotes and without quotes result is the same
The content of the conditions tested production system nfs startup script: Example 4 (production)
[root @ node1 init.d] # More /etc/init.d/rpcbind # Source Networking the Configuration. [-f / etc / sysconfig / network] &&. / etc / sysconfig / Network # → If the / etc / sysconfig / network file exists on the load file # This IS AN Interactive Program, The Current WE need the locale [-f /etc/profile.d/lang.sh] &&. /etc/profile.d/lang.sh # → If the / etc / profile there is loads the file .d / lang.sh file
Special Note: The system is the first script we learn benchmark program, to refer more, though, is not some special specifications
Example 5: examples of simple and efficient file is determined
when doing the test is determined, not necessarily according to the previous method. The latter is sometimes used directly to make a judgment more concise. E.g:
example:
[root @ node1 init.d] # [-f "$ file1"] && echo # 1 condition is true returns 1 1 [root @ node1 init.d] # [-f "$ file1"] && echo condition is not satisfied return 0 # 0 0
System example: /etc/init.d/nfs
[ -x /usr/sbin/rpc.nfsd ] || exit 5 [ -x /usr/sbin/rpc.mountd ] || exit 5 [ -x /usr/sbin/exportfs ] || exit 5