您的位置:首页 > 运维架构 > Shell

shell test command 列表

2007-03-07 14:44 323 查看
用shell编一些小程序时老是会忘记这些test命令,终于下决心把它整理出来:)

1. 数学表达式比较

-eq 相当于 ==

-ne 相当于 != not equal

-lt 相当于 less than <

-le 相当于 less than or equal <=

-gt 相当于 greater than >

-ge 相当于 greater than or equal >=

2. 字符串比较

1) 相同

if [ str1 = str2 ]; then

fi

2)不同

if [ str1 != str2 ]; then

fi

3)字符串str1 定义了且不为NULL (原文是string1 is NOT NULL or not defined , 但测试结果好象不同)

if [ str1 ]; then

fi

4)这个的原文是string1 is NOT NULL and does exist, 但测试结果是不管str1是否定义以及是否为空,该条件都成立

if [ -n str1 ]; then

fi

5)这个原文是string1 is NULL and does exist, 但测试结果是定义了为NULL,或者未定义,该条件都成立

if [ -z str1 ]; then

fi

3. 文件属性判断

-s 文件不为空

-f 是文件而不是directory

-d 是directory

-w 可以写

-r 是只读文件

-x 是可执行文件

4. 逻辑操作比较

! 相当于not such as: if [ ! 5 -gt 6 ]; then ....

-a and if [ 5 -gt 6 -a 5 -gt 7 ]; then...

-o or as above......

5. 其它

还可以测试一个命令的返回是否为真,例如:

if rm $1

then

fi

测试rm 命令的返回码.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: