Bash helps if-else statements to be able to use logical reasoning in your shell scripts.
The generic if-else syntax is like this:
if [ expression ]; then
## execute this block if situation is true else go to subsequent
elif [ expression ]; then
## execute this block if situation is true else go to subsequent
else
## if not one of the above situations are true, execute this block
fi
As you possibly can discover:
elif is used for “else if” type of conditionThe if else situations all the time finish with fithe use of semicolon ; after which key phrase
Earlier than I present the examples of if and else-if, let me share widespread comparability expressions (additionally referred to as check situations) first.
Check situations
Listed below are the check situation operators you should use for numeric comparability:
Situation
Equal to true when
$a -lt $b
$a < $b ($a is lower than $b)
$a -gt $b
$a > $b ($a is bigger than $b)
$a -le $b
$a <= $b ($a is much less or equal than $b)
$a -ge $b
$a >= $b ($a is bigger or equal than $b)
$a -eq $b
$a is the same as $b
$a -ne $b
$a shouldn’t be equal to $b
In case you are evaluating strings, you should use these check situations:
Situation
Equal to true when
“$a” = “$b”
$a is similar as $b
“$a” == “$b”
$a is similar as $b
“$a” != “$b”
$a is completely different from $b
-z “$a”
$a is empty
There are additionally situations for file kind verify:
Situation
Equal to true when
-f $a
$a is a file
-d $a
$a is a listing
-L $a
$a is a hyperlink
Now that you’re conscious of the varied comparability expressions let’s examine them in motion in varied examples.
Use if assertion in bash
Let’s create a script that tells you if a given quantity is even or not.
This is my script named even.sh:
#!/bin/bash
learn -p “Enter the quantity: ” num
mod=$(($numpercent2))
if [ $mod -eq 0 ]; then
echo “Quantity $num is even”
fi
The modulus operation (%) returns zero when it’s completely divided by the given quantity (2 on this case).
🚧
Pay particular consideration to house. There have to be house between the opening and shutting brackets and the situations. Equally, house have to be earlier than and after the conditional operators (-le, == and so forth).
This is what it exhibits after I run the script:
Did you discover that the script tells you when a quantity is even however it would not show something when the quantity is odd? Let’s enhance this script with the usage of else.
Use if else assertion
Now I add an else assertion within the earlier script. This manner once you get a non-zero modulus (as odd numbers should not divided by 2), it should enter the else block.
#!/bin/bash
learn -p “Enter the quantity: ” num
mod=$(($numpercent2))
if [ $mod -eq 0 ]; then
echo “Quantity $num is even”
else
echo “Quantity $num is odd”
fi
Let’s run it once more with the identical numbers:
As you possibly can see, the script is healthier because it additionally tells you if the quantity is odd.
Use elif (else if) assertion
This is a script that checks whether or not the given quantity is constructive or unfavorable. In arithmetic, 0 is neither constructive nor unfavorable. This script retains that reality in verify as properly.
#!/bin/bash
learn -p “Enter the quantity: ” num
if [ $num -lt 0 ]; then
echo “Quantity $num is unfavorable”
elif [ $num -gt 0 ]; then
echo “Quantity $num is constructive”
else
echo “Quantity $num is zero”
fi
Let me run it to cowl all three instances right here:
Mix a number of situations with logical operators
Thus far, so good. However have you learnt that you will have a number of situations in a single by utilizing logical operators like AND (&&), OR (||) and so forth? It offers you the flexibility to put in writing advanced situations.
Let’s write a script that tells you whether or not the given yr is a bissextile year or not.
Do you bear in mind the situations for being a bissextile year? It needs to be divided by 4 however whether it is divisible by 100, it isn’t a bissextile year. Nonetheless, whether it is divisible by 400, it’s a bissextile year.
This is my script.
#!/bin/bash
learn -p “Enter the yr: ” yr
if [[ ($(($year%4)) -eq 0 && $(($year%100)) != 0) || ($(($year%400)) -eq 0) ]]; then
echo “12 months $yr is bissextile year”
else
echo “12 months $yr is regular yr”
fi
💡
Discover the usage of double brackets [[ ]] above. It’s obligatory in case you are utilizing logical operators.
Confirm the script by operating it with completely different knowledge:
🏋️ Train time
Let’s do some exercise 🙂
Train 1: Write a bash shell script that checks the size of the string supplied to it as an argument. If no argument is supplied, it prints ’empty string’.
Train 2: Write a shell script that checks whether or not a given file exists or not. You may present the total file path because the argument or use it straight within the script.
Trace: Use -f for file
Train 3: Improve the earlier script by checking if the given file is common file, a listing or a hyperlink or if it would not exist.
Trace: Use -f, -d and -L
Train 3: Write a script that accepts two string arguments. The script ought to verify if the primary string incorporates the second argument as a substring.
Trace: Check with the earlier chapter on bash strings
Chances are you’ll focus on your resolution within the Neighborhood:
I hope you’re having fun with the Bash Fundamentals Sequence. Within the subsequent chapter, you may find out about utilizing loops in Bash. Carry on bashing!