test

条件测试常用语法

文件类型判断

选项
解释

-e

FILE exists

-d

FILE exists and is a directory

-f

FILE exists and is a regular file

-b

FILE exists and is block special

-c

FILE exists and is character special

-S

FILE exists and is a socket

-p

FILE exists and is a named pipe

-h

FILE exists and is a symbolic link (same as -L)

-L

FILE exists and is a symbolic link (same as -h)

-s

FILE exists and has a size greater than zero

[root@localhost ~]# ll
total 8
-rw-------. 1 root root 2819 Apr 25 18:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Desktop
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Documents
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Downloads
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Music
-rw-------. 1 root root 2099 Apr 25 18:37 original-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Pictures
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Public
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Templates
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Videos
[root@localhost ~]# 
[root@localhost ~]# test -d Desktop && echo yes || echo no
yes
[root@localhost ~]# test -e Desktop && echo yes || echo no
yes
[root@localhost ~]# test -f Desktop && echo yes || echo no
no
[root@localhost ~]# test -f anaconda-ks.cfg && echo yes || echo no
yes

文件权限判断

选项
解释

-r

FILE exists and read permission is granted

-w

FILE exists and write permission is granted

-x

FILE exists and execute (or search) permission is granted

-u

FILE exists and its set-user-ID bit is set

-g

FILE exists and is set-group-ID

-k

FILE exists and has its sticky bit set

两个文件之间的比较

选项
解释

-nt

FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2 文件1是否比文件2新

-ot

FILE1 -ot FILE2 FILE1 is older than FILE2 文件1是否比文件2旧

-ef

FILE1 -ef FILE2

FILE1 and FILE2 have the same device and inode numbers 文件1与文件2是否为同一个文件

[root@localhost ~]# ll
total 8
-rw-------. 1 root root 2819 Apr 25 18:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Desktop
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Documents
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Downloads
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Music
-rw-------. 1 root root 2099 Apr 25 18:37 original-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Pictures
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Public
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Templates
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Videos
[root@localhost ~]# 
[root@localhost ~]# test anaconda-ks.cfg -nt original-ks.cfg 
[root@localhost ~]# 
[root@localhost ~]# echo $?
0
[root@localhost ~]# 
[root@localhost ~]# test anaconda-ks.cfg -ot original-ks.cfg 
[root@localhost ~]# 
[root@localhost ~]# echo $?
1
[root@localhost ~]# test anaconda-ks.cfg -ef original-ks.cfg 
[root@localhost ~]# 
[root@localhost ~]# echo $?
1

两个整数之间的比较

选项
解释

-eq

INTEGER1 -eq INTEGER2 , INTEGER1 is equal to INTEGER2

-nq

INTEGER1 -ne INTEGER2 , INTEGER1 is not equal to INTEGER2

-gt

INTEGER1 -gt INTEGER2 , INTEGER1 is greater than INTEGER2

-lt

INTEGER1 -le INTEGER2 , INTEGER1 is less than or equal to INTEGER2

-ge

INTEGER1 -ge INTEGER2 , INTEGER1 is greater than or equal to INTEGER2

-le

INTEGER1 -le INTEGER2 , INTEGER1 is less than or equal to INTEGER2

字符串的判断

选项
解释

-z

-z STRING , the length of STRING is zero

-n

-n STRING , the length of STRING is nonzero

= 或 ==

STRING1 = STRING2 , the strings are equal = 和 == 一样

!=

STRING1 != STRING2 , the strings are not equal

[root@localhost ~]# test -z "" && echo yes || echo no
yes
[root@localhost ~]# test -z " " && echo yes || echo no
no
[root@localhost ~]# test -n "" && echo yes || echo no
no
[root@localhost ~]# test -n " " && echo yes || echo no
yes
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# test "hello" = "world"
[root@localhost ~]# echo $?
1
[root@localhost ~]# test "hello" = "hello"
[root@localhost ~]# echo $?
0
[root@localhost ~]# 
[root@localhost ~]# test "hello" != "world"
[root@localhost ~]# echo $?
0
[root@localhost ~]# test "hello" != "hello"
[root@localhost ~]# echo $?
1

表达式的判断

选项
解释

-a

EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true

-o

EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true

!

! EXPRESSION EXPRESSION is false 取反

[root@localhost ~]# ll
total 8
-rw-------. 1 root root 2819 Apr 25 18:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Desktop
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Documents
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Downloads
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Music
-rw-------. 1 root root 2099 Apr 25 18:37 original-ks.cfg
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Pictures
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Public
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Templates
drwxr-xr-x. 2 root root    6 Apr 27 16:20 Videos
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# test -r Desktop/ -a -x Desktop/
[root@localhost ~]# echo $?
0
[root@localhost ~]# 
[root@localhost ~]# test -r anaconda-ks.cfg -a -x anaconda-ks.cfg 
[root@localhost ~]# echo $?
1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# test -x Desktop -o -x anaconda-ks.cfg 
[root@localhost ~]# echo $?
0
[root@localhost ~]# 
[root@localhost ~]# test -x original-ks.cfg -o -x anaconda-ks.cfg 
[root@localhost ~]# echo $?
1
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# test ! -x anaconda-ks.cfg && echo yes || echo no
yes

Last updated