首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

条件测试

Posted on 2010-01-14 21:01  放飞自我  阅读(283)  评论(0编辑  收藏  举报

条件测试:主要用于ifwhile语句中用于判断。

一般格式:

test condition  [condition] 其中condition表示条件表达式。

其中包括:

1.      对文件状态的测试

-d

目录

-f

正规文件

-L

符号链接

-r

可读

-s

文件长度大于0,即非空

-w

可写

-u

文件有suid位设置

-x

可执行

返回状态可用 $?表示。比如 echo $?  

0表示正确,1表示错误。

 

2.      逻辑关系

-a

逻辑与

-o

逻辑或

              例如:[ -x result.txt –o –x source.txt ] echo $?

 

3.      字符串测试

[ string string_operator string]

其中 string_operator可以为:

        =                相等

        !=                  不等

        -z                  空串

        -n                  非空串

可用于测试某个环境变量是否设置。

 

4.      测试数值

[ “number” numeric_operator “number” ]

其中 numeric_operator 可为:

-eq

-ne

-gt

-lt

-ge