shell常用的判断条件

1、判断文件夹是否存在
if [ -d /home/q/www ];then echo "true"; else echo "false" ;fi
(系统内存在文件夹 /home/q/www)

2、判断文件是否存在
if [ -e /etc/passwd ];then echo "true"; else echo "false" ;fi
if [ -f /etc/passwd ];then echo "true"; else echo "false" ;fi
文件存在且大小大于0
if [ -s /etc/passwd ];then echo "true"; else echo "false" ;fi

3、判断文件可读
if [ -r /etc/passwd ];then echo "true"; else echo "false" ;fi

4、判断文件可写
if [ -w /etc/passwd ];then echo "true"; else echo "false" ;fi

5、判断文件可执行
if [ -x /etc/passwd ];then echo "true"; else echo "false" ;fi

6、字符串长度为0,返回真
长度为0
if [ -z "" ];then echo "true"; else echo "false" ;fi
长度不为0
if [ -z "xiaoqiang" ];then echo "true"; else echo "false" ;fi

7、字符串长度不为0,返回真
长度不为0
if [ -n "xiaoqiang" ];then echo "true"; else echo "false" ;fi
长度为0
if [ -n "" ];then echo "true"; else echo "false" ;fi

8、判断字符串相等
if [ "xiaoqiang" = "xiaoqiang" ];then echo "true"; else echo "false" ;fi

9、判断字符串不相等
if [ "xiaoqiang" != "xiaoqiang" ];then echo "true"; else echo "false" ;fi

10、判断整数相等
if [ 1 -eq 1 ];then echo "true"; else echo "false" ;fi

11、判断整数不相等
if [ 1 -ne 1 ];then echo "true"; else echo "false" ;fi

12、判断整数 1 < 2
if [ 1 -lt 2 ];then echo "true"; else echo "false" ;fi

13、判断整数 2 <= 2
if [ 2 -le 2 ];then echo "true"; else echo "false" ;fi

14、判断整数3 > 2
if [ 3 -gt 2 ];then echo "true"; else echo "false" ;fi

15、判断整数 3 >= 3
if [ 3 -ge 3 ];then echo "true"; else echo "false" ;fi

16、逻辑运算
!(非)
if [ ! 3 -ge 3 ];then echo "true"; else echo "false" ;fi

-a(and)
 if [ -z "" -a 3 -ge 3 ];then echo "true"; else echo "false" ;fi

 -o(或)
if [ -z "sadfsa" -o 3 -ge 3 ];then echo "true"; else echo "false" ;fi

17、特殊变量
echo "number:$#"    获取参数个数
echo "scname:$0"    获取脚本名称
echo "first:$1"            获取第一个参数
echo "second:$2"         获取第二个参数
echo "argume:$@"         获取所有参数
echo "show parm list :$*" 数据参数的list
echo "show process id :$$"  获取进程id
echo "show precomm stat:$?"     获取命令执行的结果

 

posted @ 2014-11-18 11:30  liuweiqiang2v  阅读(235)  评论(0编辑  收藏  举报