if

1.test

测试范围:整数,字符串,文件

表达式结果为真,则test的返回值为0,否则为非0。

表达式结果为真,则变量$?的值为0,否则为非0

 

(一)字符串

判断是否相等:

     test $str1 == $str2 ;echo $?

     test $str1 != $str2;echo $?

判断是否有值:有则返回0

    test $str1;echo $?

    test -n $str1;echo $?

 判断是否为空:

   test -z $str1;echo $?

(二)整数

test int1  -eq int2

等于   -eq    --equal

大于等于    ge   --greater equal

大于  gt   --greater than

小于等于   le   --less equal

小于   lt  --less than

不等于 ne

 

(三)文件

test -d file

-e 是否存在(exists)

-d   是否为目录

-f 是否常规文件

-L 是否时=是链接文件

-r 是否可读文件

-w 是否可写文件

-x 是否可执行文件

(四)逻辑判断

-a  all 都为真,结果为真

-o  or,有一个为真,结果为真

! 取反

[ -d qiao -o -f var.log ];echo $?

[ -d qiao -o -f var.log ] && echo "succed" || echo "faild"

 

posted on 2019-11-03 19:06  happygril3  阅读(131)  评论(0)    收藏  举报

导航