首页 > 代码库 > shell常用的判断条件

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" ;fiif [ -f /etc/passwd ];then echo "true"; else echo "false" ;fi文件存在且大小大于0if [ -s /etc/passwd ];then echo "true"; else echo "false" ;fi3、判断文件可读if [ -r /etc/passwd ];then echo "true"; else echo "false" ;fi4、判断文件可写if [ -w /etc/passwd ];then echo "true"; else echo "false" ;fi5、判断文件可执行if [ -x /etc/passwd ];then echo "true"; else echo "false" ;fi6、字符串长度为0,返回真长度为0if [ -z "" ];then echo "true"; else echo "false" ;fi长度不为0if [ -z "xiaoqiang" ];then echo "true"; else echo "false" ;fi7、字符串长度不为0,返回真长度不为0if [ -n "xiaoqiang" ];then echo "true"; else echo "false" ;fi长度为0if [ -n "" ];then echo "true"; else echo "false" ;fi8、判断字符串相等if [ "xiaoqiang" = "xiaoqiang" ];then echo "true"; else echo "false" ;fi9、判断字符串不相等if [ "xiaoqiang" != "xiaoqiang" ];then echo "true"; else echo "false" ;fi10、判断整数相等if [ 1 -eq 1 ];then echo "true"; else echo "false" ;fi11、判断整数不相等if [ 1 -ne 1 ];then echo "true"; else echo "false" ;fi12、判断整数 1 < 2if [ 1 -lt 2 ];then echo "true"; else echo "false" ;fi13、判断整数 2 <= 2if [ 2 -le 2 ];then echo "true"; else echo "false" ;fi14、判断整数3 > 2if [ 3 -gt 2 ];then echo "true"; else echo "false" ;fi15、判断整数 3 >= 3if [ 3 -ge 3 ];then echo "true"; else echo "false" ;fi16、逻辑运算!(非)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" ;fi17、特殊变量echo "number:$#"    获取参数个数echo "scname:$0"    获取脚本名称echo "first:$1"            获取第一个参数echo "second:$2"         获取第二个参数echo "argume:$@"         获取所有参数echo "show parm list :$*" 数据参数的listecho "show process id :$$"  获取进程idecho "show precomm stat:$?"     获取命令执行的结果

 

shell常用的判断条件