Logical OR & AND operations are very useful when using multiple conditions in our programs (scripts). Or used between two or more conditions. If any of the conditions returns true, then true is returned. Used between two or more conditions. True will be returned only if all conditions return true.
Use logical OR
Logical OR is used with operator -o in a bash script. The following shell script will show how to use logical OR (-o) between two conditions.
#!/bin/bash read -p "Enter First Numeric Value: " first read -p "Enter Second Numeric Value: " second if [ $first -le 10-o$second -gt 20 ] then echo "OK" else echo "Not OK" fi
Use logical with
Logical AND and operator-a in bash script. The following shell script will show how to use logical AND (-a) between two conditions.
#!/bin/bash read -p "Enter First Numeric Value: " first read -p "Enter Second Numeric Value: " second if [ $first -le 10-a$second -gt 20 ] then echo "OK" else echo "Not OK" fi
Logical OR & mixed with
The following example shows how to use multiple logical operators in a single statement.
#!/bin/bash read -p "Enter Your Number:" i if [ ( $i -ge 10 -a $i -le 20 ) -o ( $i -ge 100 -a $i -le 200 ) ] then echo "OK" else echo "Not OK" fi
This article has ended here. For more other exciting content, you can pay attention to the Linux Tutorial Video column of the PHP Chinese website!
The above is the detailed content of How to use logical OR & and in shell script. For more information, please follow other related articles on the PHP Chinese website!