bash - Logical_OR

转载

https://bash.cyberciti.biz/guide/Logical_OR

 

Logical OR

 

 
← Logical ANDHomeLogical Not ! →

Logical OR (||) is boolean operator. It can execute commands or shell functions based on the exit status of another command.

Syntax

command1 || command2

OR

First_command || Second_command

command2 is executed if, and only if, command1 returns a non-zero exit status. In other words, run command1 successfully or run command2.

Example

cat /etc/shadow 2>/dev/null || echo "Failed to open file"

The cat command will try to display /etc/shadow file and it (the cat command) sets the exit stats to non-zero value if it failed to open /etc/shadow file. Therefore, 'Failed to open file' will be displayed cat command failed to open the file.

Find username else display an error
grep "^vivek" /etc/passwd || echo "User vivek not found in /etc/passwd"

How Do I Combine Both Logical Operators?

Try it as follows:

cat /etc/shadow 2>/dev/null && echo "File successfully opened." || echo "Failed to open file." 

Make sure only root can run this script:

test $(id -u) -eq 0  && echo "You are root" || echo "You are NOT root" 

OR

test $(id -u) -eq 0  && echo "Root user can run this script." || echo "Use sudo or su to become a root user." 

External links

← Logical AND

转载于:https://www.cnblogs.com/itzxy/p/6338150.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值