Shell script for conditional if-else block

Table of contents

Last Blog Review →
In the last blog we understood,
Using bash to execute the shell script If we use "bash filename.sh" it will just run the shell script and doesn't execute. So, even if the executable permissions are not there still the
Making the shell script as a command to run we can use it.
Running the shell script using .sh Always use ./filename.sh command to execute a shell script file, because "./filename.sh" will execute the shell script and to execute the shell script we need to give the permission using "chmod" command to execute.
If else statement -
Syntax -
if [ stringa = stringb ] then ..... elif [ string1 = string2 ] then ..... else .... fi
Comparison Operators -
To compare the strings we us “=” or “ != ” operators i.e. [ 'abc' = 'abc'], [ 'abc' != 'abc' ]
To compare the integers we use “-eq”, “-ne“, “-lt”, “-gt” i.e. [ 5 -eq 5 ], [ 5 -ne 6 ], [ 5 -lt 4 ], [ 5 -gt 3 ]
There is another way of specifying a conditional operation and that's using double square brackets [[ ]]. These are enhanced version and supports additional operations like matching patterns using expressions and they are a bash extension so it only works in bash or other supported modern shells.
3. 1 Ex.1 : [[ "abcd" = bc ]] If abcd contains bc (true)
3.2 Ex.2 : [[ "abc" = ab[cd] ]] If 3rd character of abc/abd is c or d (true) OR
[[ "abd" = ab[cd] ]]
3.3 Ex.3 : [[ "abe" = "ab[cd]" ]] If 3rd character of abe is c or d (false)
3.4 Ex.4 : [[ "abc" > "bcd" ]] If "abc" comes after "bcd" when sorted in alphabetical (lexographical) order (false). Here '>' is not greater than symbol but used to compare the position of these strings if they were sorted alphabetically Ex.5 [[ "abc" < "bcd" ]] If "abc" comes before "bcd" when sorted in alphabetical (lexographical) order (true). Similarly for '<'.
Conditional Operators :
AND comparison operator -
AND Operators using double square brackets : To check if both the conditions are true use double && sign.
Ex. [ cond1 && cond2 ]
Ex. [[ A -gt 4 && A -lt 10 ]] If A is greater than 4 and less than 10
OR comparison operator -
OR Operators using double square brackets : To check if either one is true then use double || sign.
Ex. [ cond1 || cond2 ]
Ex. [[ A -gt 4 || A -lt 10 ]] If A is greater than 4 or less than 10
File level operators -
[ -e FILE ] or [ -f FILE ] = if FILE exists
[ -d FILE ] = if FILE exists and is a directory or checks if given path is a directory
[ -s FILE ] = if FILE exists and has size greater than 0
[ -x FILE ] = if FILE is executable
[ -w FILE ] = if FILE is writable.
Conclusion →
In the this blog we understood, what if conditional logic and how to write the syntax for if—else statement in shell script. Also we checked how the different operators for checking the conditions are used to get perform if-else.
Cover credit → https://images.app.goo.gl/viUJZ2cWCVKBDFKN9
Subscribe to my newsletter
Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mihir Suratwala
Mihir Suratwala
Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.