码迷,mamicode.com
首页 > 其他好文 > 详细

bash - Logical_OR

时间:2017-01-22 07:49:45      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:val   bool   syntax   home   opera   boolean   use   can   roo   

转载

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

 

Logical OR

 

 
← Logical AND Home Logical 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

bash - Logical_OR

标签:val   bool   syntax   home   opera   boolean   use   can   roo   

原文地址:http://www.cnblogs.com/itzxy/p/6338150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!