linux中shell命令判断,linux之shell命令——条件判断(&& || test)-Go语言中文社区...

&& 和 ||

&&:用来执行条件成立后执行的命令

||:用来执行条件不成立后的执行命令

示例:

145de34689343b37ebcb1962f5c29b65.png

[root@localhost mnt]# id toto # 用户toto不存在无法查看

id: toto: no such user

[root@localhost mnt]# id toto &> /dev/null && echo yes || echo no # 判断用户是否存在

no # 由于用户不存在 条件不成立 所以输出no

[root@localhost mnt]# useradd toto # 创建用户之后 用户存在

[root@localhost mnt]# id toto &> /dev/null && echo yes || echo no # 条件成立 输出yes

yes

测试:检测是否能够ping通 主机 172.25.47.204 ,通则输出yes,不通则输出no

0db5ab68e5371e35cd35981953a38b0e.png

编写脚本,用以检测网络能否ping通

可以ping通输出此ip is up;ping不通输出 此ip is down

feed421d9f86fefc556b3e98ced85fdb.png

2998c4fa2618622cb071a2bae38ae8ee.png

test 命令

test命令通常做判断, test 命令和 [ ] 等同

[ "$A" = "$B" ] ##是否相等

[ "$A" != "$B" ] ##是否不相等

[ "$A" -eq "$B" ] ##是否相等

[ "$A" -ne "$B" ] ##是否不相等

[ "$A" -le "$B" ] ##是否小于等于

[ "$A" -lt "$B" ] ##是否小于

[ "$A" -ge "$B " ] ##是否大于等于

[ "$A" -gt "$B" ] ##是否大于

[ "$A" -ne "$B" -a "$A" -gt "$B" ] ##A 不等于 B 并且 A 大于B

[ "$A" -ne "$B" -o "$A" -gt "$B" ] ##A不等于B 或者 A大于B

[ -z "$A" ] ##是否为空

[ -n "$A" ] ##是否不为空

示例

1 test命令通常做判断, test 命令和 [ ] 等同

[root@localhost mnt]# a=1

[root@localhost mnt]# b=1

[root@localhost mnt]# test "$a" = "$b" && echo yes || echo no

yes

[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no

yes

[root@localhost mnt]# test "$a" != "$b" && echo yes || echo no

no

[root@localhost mnt]# [ "$a" != "$b" ] && echo yes || echo no

no

2 -eq ##等于

[root@localhost mnt]# a=1

[root@localhost mnt]# b=1

[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no

yes

[root@localhost mnt]# [ "$a" -eq "$b" ] && echo yes || echo no

yes

3 -ne ##不等于

[root@localhost mnt]# a=1

[root@localhost mnt]# b=1

[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no

no

[root@localhost mnt]# b=2

[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no

yes

4 -le ##小于等于

[root@localhost mnt]# a=1

[root@localhost mnt]# b=2

[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no

yes

[root@localhost mnt]# b=1

[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no

yes

[root@localhost mnt]# b=0

[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no

no

5 -lt ##小于

[root@localhost mnt]# a=1

[root@localhost mnt]# b=2

[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no

yes

[root@localhost mnt]# b=1

[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no

no

6 -ge ##大于等于

[root@localhost mnt]# a=1

[root@localhost mnt]# b=2

[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no

no

[root@localhost mnt]# a=2

[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no

yes

[root@localhost mnt]# a=3

[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no

yes

7 -gt ##大于

[root@localhost mnt]# a=1

[root@localhost mnt]# b=1

[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no

no

[root@localhost mnt]# a=2

[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no

yes

8 -a ##同时满足

[root@localhost mnt]# a=1

[root@localhost mnt]# b=2

[root@localhost mnt]# c=3

[root@localhost mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no

yes

[root@localhost mnt]# b=1

[root@localhost mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no

no

9 -o ##两个条件满足其中一个就可以

[root@localhost mnt]# a=1

[root@localhost mnt]# b=2

[root@localhost mnt]# c=3

[root@localhost mnt]# [ "$a" -lt "$b" -o "$b" -gt "$c" ] && echo yes || echo no

yes

[root@localhost mnt]# [ "$a" -gt "$b" -o "$b" -gt "$c" ] && echo yes || echo no

no

10 -z ##是否为空

[root@localhost mnt]# c=''

[root@localhost mnt]# [ -z "$c" ] && echo yes || echo no

yes

[root@localhost mnt]# c=2

[root@localhost mnt]# [ -z "$c" ] && echo yes || echo no

no

11 -n ##不为空

[root@localhost mnt]# c=''

[root@localhost mnt]# [ -n "$c" ] && echo yes || echo no

no

[root@localhost mnt]# c=2

[root@localhost mnt]# [ -n "$c" ] && echo yes || echo no

yes

一个文件具体属于什么类型

[ -f “file” ] ##普通文件为真

[ -e “file” ] ##文件存在为真

[ -L “file” ] ##软链接为真

[ -S “file” ] ##套节字为真

[ -b “file” ] ##硬盘设备为真

[ -d “file” ] ##目录为真

[ -c “file” ] ##字符设备为真

[ “file1” -ef “file2” ] ##两文件为同一个文件为真

[ “file1” -nt"file2" ] ##文件1比文件2创建的晚为真

[ “file1” -ot “file2” ] ##文件1比文件2创建的早为真

示例:

[ “file1” -ef “file2” ] ##两文件为同一个文件为真

[root@localhost mnt]# touch file1

[root@localhost mnt]# ln file1 file2

[root@localhost mnt]# ls

file1 file2 ping.sh

[root@localhost mnt]# ls -i

8845808 file1 8845808 file2 8845813 ping.sh

[root@localhost mnt]# [ "file1" -ef "file2" ] && echo yes || echo no

yes

[root@localhost mnt]# [ "file1" -ef "ping.sh" ] && echo yes || echo no

no

[ “file1” -nt"file2" ] ##文件1比文件2创建的晚为真

[ “file1” -ot “file2” ] ##文件1比文件2创建的早为真

[root@localhost mnt]# ll

total 0

-rw-r--r--. 1 root root 0 May 20 06:25 file1

-rw-r--r--. 1 root root 0 May 20 06:27 file2

[root@localhost mnt]# [ "file1" -nt "file2" ] && echo yes || echo no

no

[root@localhost mnt]# [ "file1" -ot "file2" ] && echo yes || echo no

yes

测试1 编写脚本 判断输出文件类型

9ba5ca5cfadb2bac811458a0f7fc5f6c.png

33f6ca0e579e72b9993d626d0f8e7e27.png

测试2 编写脚本 创建或者删除用户

当创建用户时,用户存在则输出用户存在,不存在则建立用户

当删除用户时,用户存在则删除用户,用户不存在则输出用户不存在

aa22beabd5ad90bc56f7677decc05070.png

69cc33f129ca8d2fe7ae637276cba0d3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值