Shell && vs || operator

cmd1 && cmd2 如果命令cmd1 的result status 0,则执行cmd2,否则不执行cmd2命令。
cmd1 || cmd2 如果命令cmd1 的result status 1,则执行cmd2, 否则不执行cmd2命令。
可以用于异常出错处理:
i.e: 运行cmd1命令,如果cmd1运行出错,则输出一条提示信息。
adb -s "device" logcat || { echo "device not found"; exit 1; }

解释:如果命令adb -s "device" logcat 执行出错, 则给用户提示"device not found."

i.e:
如下命令:
[[ -e file1 ]] && echo "file1 file exits." || touch file1

第一次执行该命令,由于当前命令下没有file1 file,执行[[ -e file1 ]] 命令的result status 为1 ; 不会执行命令echo "file1 file exits", 而对于touch file1 命令,由于之前的操作([[ -e file1 ]] && echo "file1 file exits.")的 result status 为1,因此,则执行touch file1

但运行第二次该命令时,[[ -e file1 ]] 命令返回0, 则执行echo "file1 file exits" ,而不执行touch file1命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值