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

文件权限判断

选项
解释

-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是否为同一个文件

两个整数之间的比较

选项
解释

-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

表达式的判断

选项
解释

-a

EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true

-o

EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true

!

! EXPRESSION EXPRESSION is false 取反

Last updated