3. Compound comparison operators

 

In bash, there are two operators which could work as logical or and logical or operators. These operators used to make choice between two or more expression. These are mainly two types
operators, Let’s describe and see some of their examples.

1. -o Logical or operator
2. -a Logical and operator

or operator

In Logical or operator, we could use two or more expression, if any of expression is true, then returns true.

#[ 3 -le 4 -o 4 -le 5 ]; echo $?
0
#[ 6 -le 4 -o 4 -le 5 ]; echo $?
0
#[ 6 -le 4 -o 6 -le 5 ]; echo $?
1

In above box, we can see that we have used three examples with two expression. In first both expression are true, so it returns true. In second one of expression is true, so it returns true andin third none of expression is true, so it returns false.
we can use more than two expression as well sam as we used two above. Let’s see some examples.

#[ 3 -le 4 -o 4 -le 5 -o 5 -lt 6 -o 35 -ge 23 ]; echo $?
0
#[ 3 -eq 4 -o 4 -lt 5 -o 5 -lt 6 -o 35 -ge 23 -o 87 -gt 76 ]; echo $?
0
#[ 3 -eq 4 -o 6 -lt 5 -o 7 -lt 6 -o 21 -ge 23 -o 67 -gt 76 ]; echo $?
1

We can use them for string operations as well, Let’s try some string operations with some if conditions in scriot format.

#!/bin/bash
read -p "Do your want to continue with this script:(Y/N) " answer
if [ $answer == y -o $answer == Y -o $answer == yes -o $answer == Yes -o $answer == YES -o $answer == YEs -o $answer == yES -o $answer == yeS -o $answer == yEs ]; then
echo " OK. we continue with this script "
else
echo " EXIT "
fi
=====================================
OutPut
#./or_opeator
Do your want to continue with this script:(Y/N) y
OK. we continue with this script
#./or_opeator
Do your want to continue with this script:(Y/N) Y
OK. we continue with this script
#./or_opeator
Do your want to continue with this script:(Y/N) Yes
OK. we continue with this script
#./or_opeator
Do your want to continue with this script:(Y/N) YEs
OK. we continue with this script
#./or_opeator
Do your want to continue with this script:(Y/N) YES
OK. we continue with this script
#./or_opeator
Do your want to continue with this script:(Y/N) N

With all above explanation and examples, we can say that, yes we know how or comparison operator work and how we can use it normal day to day system administrator operations.

and operator

 

In Logical and operator, we could use two or more expression, if all of expression is true, then returns true.

#[ 3 -le 4 -a 4 -le 5 ]; echo $?
0
#[ 3 -le 4 -a 6 -le 5 ]; echo $?
1

In above examples, we can see when all expression are true, only when it returns true.
we can use more than two expression as we did above with or operators.

4. Checking exit status

In our previous examples, we have already used exit status many times. So you may already know what is exit status and how we could identify this. Now important thing is know significance of exit status and how to use this.
Exit status will provide status of last command run and on basis of that status we can decide flow of script and execute some task. exit status is carried by “$?
As we already covered it many times, so here we are not going deep in this, just explaining with one liner script.

#ls -l /etc/fstab
-rw-r--r-- 1 root root 664 Aug 18 09:19 /etc/fstab
#if [ $? -eq 0 ]; then echo "Last command run successfully" ; else echo "Last command wasn't run successfully"; fi
Last command run successfully
#ls -l /bin/fstab
ls: cannot access /bin/fstab: No such file or directory
#if [ $? -eq 0 ]; then echo "Last command run successfully" ; else echo "Last command wasn't run successfully"; fi
Last command wasn't run successfully

In above example, we can say exit status can help us to take some decision on that basis we can execute some task.