找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 199|回复: 0

shell的 test 命令使用

[复制链接]

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
发表于 2013-8-21 18:17:36 | 显示全部楼层 |阅读模式

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
判断 file2 file2 是否为同一文件,可用在判断 hard link 的判定上。 主要意义在判定,两个文件是否均指向同一个 inode 哩!

4. 关于两个整数之间的判定,例如 test n1 -eq n2  
-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)

5. 判定字符串的数据  
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

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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 14:05 , Processed in 0.062364 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表