Shell判断

shell 条件测试:


文件测试 【操作符 文件或目录】:
test 条件表达式
【条件表达式】
【【条件表达式】】

-b filename : 当filename 存在并且是块文件时返回真(返回0)
-c filename : 当filename 存在并且是字符文件时返回真
-d pathname : 当pathname 存在并且是一个目录时返回真
-e pathname : 当由pathname 指定的文件或目录存在时返回真
-f filename : 当filename 存在时返回真
-g pathname : 当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真
-h filename : 当filename 存在并且是符号链接文件时返回真 (或 -L filename)
-k pathname : 当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真
-p filename : 当filename 存在并且是命名管道时返回真
-r pathname : 当由pathname 指定的文件或目录存在并且可读时返回真
-s filename : 当filename 存在并且文件大小大于0 时返回真
-S filename : 当filename 存在并且是socket 时返回真
-t fd : 当fd 是与终端设备相关联的文件描述符时返回真
-u pathname : 当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真
-w pathname : 当由pathname 指定的文件或目录存在并且可写时返回真
-x pathname : 当由pathname 指定的文件或目录存在并且可执行时返回真
-O pathname : 当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)
-G pathname : 当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真
file1 -nt file2 : file1 比file2 新时返回真
file1 -ot file2 : file1 比file2 旧时返回真

数值比较 【整数1 操作符 整数2】:
-gt 大于 -lt 小于 -eq 等于 -ne 不等于 -ge 大于等于 -le 小于等于
((整数1 操作符 整数2)) c语言表达式
【【 数1 操作符 数2 】】 正则表达式

字符串比较 [ "字符串" = "字符串" ]:
=,== 等于 != 不等于
-z:判断字符长度是为0返回真
-n:判断字符长度不是为0返回真
变量为空或未定义长度都为0

and和or:
&& 逻辑的 AND 的意思, -a 也是这个意思,两个条件同时成立,为真。 -a
|| 逻辑的 OR 的意思, -o 也是这个意思,两个条件一个成立,为真。 -o

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if


单分支结构:
if [条件测试];
then 命令序列
fi

双分支结构:
if [条件测试] ;
then 命令序列
else 命令序列
fi

多分支结构:
if [条件测试1];
then 命令序列

elif [条件测试2];
then 命令序列

elif [条件测试3];
then 命令序列...

else 命令序列
fi

嵌套结构:
if [条件测试1]; then 命令序列
if [条件测试1]; then 命令序列

else 命令序列
fi

else 命令序列
fi

case


case 语法结构(字符串比较):
case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
无匹配后命令序列
esac

猜你喜欢

转载自blog.csdn.net/qq_30429153/article/details/85242913