Linux test 命令使用
目录

test 命令用于各类条件侦测,是 shell 脚本里最常用的判断工具。
1. 文件类型侦测(是否存在)
如 test -e filename:
-e该文件名是否存在?(常用)-f是否为文件(file)?(常用)-d是否为目录(directory)?(常用)-b是否为一个 block device 装置?-c是否为一个 character device 装置?-S是否为一个 Socket 文件?-p是否为一个 FIFO(pipe)文件?-L是否为一个连结档?
2. 文件权限侦测
如 test -r filename:
-r是否具有『可读』属性?-w是否具有『可写』属性?-x是否具有『可执行』属性?-u是否具有『SUID』属性?-g是否具有『SGID』属性?-k是否具有『Sticky bit』属性?-s是否为『非空白文件』?
3. 两个文件之间的比较
如 test file1 -nt file2:
-nt(newer than)判断 file1 是否比 file2 新-ot(older than)判断 file1 是否比 file2 旧-ef判断两文件是否指向同一个 inode(可用于 hard link 判定)
4. 两个整数之间的判定
如 test n1 -eq n2:
-eq两数值相等(equal)-ne两数值不等(not equal)-gtn1 大于 n2(greater than)-ltn1 小于 n2(less than)-gen1 大于等于 n2(greater than or equal)-len1 小于等于 n2(less than or equal)
5. 字符串判定
test -z string判定字符串是否为空,若为空则为 truetest -n string判定字符串是否非空,若为空则为 false(-n可省略)test str1 = str2判定 str1 是否等于 str2,相等回传 truetest str1 != str2判定 str1 是否不等于 str2
6. 多重条件判定
如 test -r filename -a -x filename:
-a(and)两状况同时成立。如test -r file -a -x file,file 同时具有 r 与 x 权限时才回传 true-o(or)两状况任一成立。如test -r file -o -x file,file 具有 r 或 x 权限就回传 true!反相状态。如test ! -x file,当 file 不具有 x 时回传 true