SHELL TEST命令[文件判断,字符串判断,整型判断]

SHELL TEST命令[文件判断,字符串判断,整型判断]

test命令

test命令,test命令是一个经常与if 一块使用的命令,这个test 命令执行各种各样的检查与比较。它有两种等价模式:

test expression
比较流行的格式是:
[ expression ]   # 注意空格

这里的expression 是一个表达式,其执行结果是true 或者是false。当表达式为真时,这个test 命令返回一个零退出状态,当表达式为假时,test 命令退出状态为1。

文件表达式

以下表达式被用来计算文件状态:

表达式如果为真
file1 -ef file2file1 和file2拥有相同的索引号(通过硬链接两个文件名指向相同的文件)。
file1 -nt file2file1 新于file2。
-b filefile 存在并且是一个块(设备)文件。
-c filefile 存在并且是一个字符(设备)文件。
-d filefile 存在并且是一个目录。
-e filefile 存在。
-f filefile 存在并且是一个普通文件。
-g filefile 存在并且设置了组ID。
-G filefile 存在并且由有效组ID 拥有。
-k filefile 存在并且设置了它的“sticky bit”。
-L filefile 存在并且是一个符号链接。
-O filefile 存在并且由有效用户ID 拥有。
-p filefile 存在并且是一个命名管道。
-r filefile 存在并且可读(有效用户有可读权限)。
-s filefile 存在且其长度大于零。
-S filefile 存在且是一个网络socket。
-t fdfd 是一个定向到终端/从终端定向的文件描述符。这可以被用来决定是否重定向了标准输入/输出错误。
-u filefile 存在并且设置了setuid 位。
-w filefile 存在并且可写(有效用户拥有可写权限)。
-x filefile 存在并且可执行(有效用户有执行/搜索权限)。

字符串表达式

表达式如果为真
stringstring 不为null。
-n string字符串string 的长度大于零。
-z string字符串string 的长度为零。
string1 = string2string1 == string2string1 和string2 相同.单或双等号都可以,不过双等号更受欢迎。
string1 != string2string1 和string2 不相同。
string1 > string2sting1 排列在string2 之后。
string1 < string2string1 排列在string2 之前。

整型表达式

表达式如果为真
integer1 -eq integer2integer1 等于integer2.
integer1 -ne integer2integer1 不等于integer2.
integer1 -le integer2integer1 小于或等于integer2.
integer1 -lt integer2integer1 小于integer2.
integer1 -ge integer2integer1 大于或等于integer2.
integer1 -gt integer2integer1 大于integer2.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值