Linux 常用命令 test

test 这个命令在linux中很常用,特别是shell编程中

命令选项:

-e  档名存在

-f   档案存在且为文件

-d  档案存在且为目录

-b 该『档名』是否存在且为一个 block device 装置?

 -c 该『档名』是否存在且为一个 character device 装置?

-S 该『档名』是否存在且为一个 Socket 档案?

-p 该『档名』是否存在且为一个 FIFO (pipe) 档案?

 -L 该『档名』是否存在且为一个连结档?

-r 侦测该档名是否存在且具有『可读』的权限?

 -w 侦测该档名是否存在且具有『可写』的权限?

-x 侦测该档名是否存在且具有『可执行』的权限?

 -u 侦测该文件名是否存在且具有『SUID』的属性?

-g 侦测该文件名是否存在且具有『SGID』的属性?

-k 侦测该文件名是否存在且具有『Sticky bit』的属性?

 -s 侦测该档名是否存在且为『非空白档案』?

-nt (newer than)判断 file1 是否比 file2 新

 -ot (older than)判断 file1 是否比 file2 旧

-ef 刞断 file1 不 file2 是否为同一档案,可用在判断 hard link 的刞定上。 主要意丿在刞定,两个档案是否均指向同一个 inode 哩!

-eq 两数值相等 (equal)

-ne 两数值丌等 (not equal)

-gt n1 大于 n2 (greater than)

-lt n1 小于 n2 (less than)

-ge n1 大于等亍 n2 (greater than or equal)

-le n1 小于等亍 n2 (less than or equal)

test -z string 刞定字符串是否为 0 ?若 string 为穸字符串,则为 true

test -n string 刞定字符串是否非为 0 ?若 string 为穸字符串,则为 false。 注: -n 亦可省略 test str1 = str2 刞定 str1 是否等亍 str2 ,若相等,则回传 true test str1 != str2 刞定 str1 是否丌等亍 str2 ,若相等,则回传 false

-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

猜你喜欢

转载自blog.csdn.net/vs2008aspnet/article/details/18553429