shell 条件判断式语句

xiaoxiao2021-02-28  99

shell两种判断格式

test -e /root/install.log

[ -e /root/install.log] 最常用

[ -d /root ] && echo "yes" || echo "no" #第一个判断命令如果正确执行,则打印"yes",否则打印"no"

In /root/student.txt /tmp/stu.txt

#创建个硬链接吧

[/root/student.txt -ef /tmp/stu.txt] && echo "yes" || echo "no"

#用test测试下,果然很有用

[23 -ge 22] && echo "yes" || echo "no" #判断23是否大于等于22,当然是了

[23 -le 22] && echo "yes" || echo "no" #判断23是否小于等于22,当然不是了

name = ff  #给name变量赋值

[ -z "$name"] && echo "yes" || echo "no"  #判断name变量是否为空,因为不为空,所以返回no

aa =11

bb = 22 #给变量aa和变量bb赋值

["$aa" == "$bb"] && echo "yes" || echo "no" #判断两个变量的值是否相等,明显不想等,所以返回no

aa=11

[-n "$aa" -a "$aa" -gt 23] && echo "yes" || echo "no"

#判断变量aa是否有值,同时判断变量aa的值是否大于23

#因为变量aa的值不大于23,所以虽然第一个判断值为真,返回的结果也是假

转载请注明原文地址: https://www.6miu.com/read-29212.html

最新回复(0)