We have already discussed about the conditional statements i.e. if-else, if-then-else & elif statements & have also discussed some basic shell scripts examples for those as well. In this tutorial, we will discuss some advanced uses of conditional statements by using logical operators in shell scripts.
Recommended Read: Bash Scripting-7- Using FOR loop in shell script
Also Read: Recommended guide to install POSTGRESQL from Source
Logical operators or boolean operators are used for performing logical operations. Following 3 are types of logical operators,
1- Logical AND (&&):- will compare two inputs & if both are true, it will return true or else false.
2- Logical OR (||):- Will check two conditions will return true, if any of them is true & return false when both are false.
3- Not equal to (!):- will return true when the condition is false & return false if the condition is true.
Using logical operators in shell scripts
Let’s see an example to get a better understanding of & using logical operators in shell scripts,
#!/bin/bash
dir=/home/ec2-user
if [ -d $dir ] && [ -w $dir ]
then
echo “$dir directory exists & its writable”
fi
So in this script, we are checking if a directory exists & if it’s readable. So when both of these conditions are satisfied, it will print the message that the directory exists & is readable. Here we can also repace AND logical operator with OR logical operator, the script will check if either directory is readable or is writable or both and will then print the message but will print nothing, if both conditions are false.
Relational Operators
Relational operators are used to check relation between two operands & depending upon the conditions, it will return a negative or positive outcome. Following is the list of relational operators,
Relational Operators |
|
<= |
less than or equal to |
>= |
greater than or equal to |
< |
less than |
> |
greater than |
== |
Equal to |
!= |
not equal to |
These relational operators are used inside double parenthesis i.e. (( )).
Double Parenthesis (( ))
These are used to performing/handling advanced arithmetic operations & following paramters can be used with this,
val ++ |
post-increment |
val — |
post decrement |
++val |
pre-increment |
–val |
pre decrement |
! |
logical NOT |
&& |
Logical AND |
|| |
Logical OR |
** |
exponential |
~ |
bitwise NOT operator |
& |
bitwise AND operator |
| |
bitwise OR operator |
<< |
left bitwise shift |
>> |
right bitwise shift |
Now let’s discuss an example of these concepts.
#!/bin/bash
num1=15
if (( $num1 ** 2 > 100 ))
then
(( num2=$number ** 2 ))
echo “Square of $num1 is $num2”
fi
This simple script checks if the square of a number is greater than 100 if it is then it will calculate the square of the number & will print with a message.
Double Brackets [[ ]]
Double parenthesis “[[ ]]” is an advanced version “[ ]” & provides us with advanced features for string comparisons.
#!/bin/bash
If [[ $USER == ec2* ]]
then
echo “Welcome $USER”
else
echo “Who are you $USER”
fi
With this script, we are checking if there is any user that starts with ‘ec2’ & if there is any user starting with ec2, it will print a welcome message or else will ask for who the user is.
With this, we end our tutorial on using logical operators in shell scripts & also performing advanced operations for arithmetic & string comparisons. Please feel free to send in any questions, queries & suggestions using the comment box below.