Here is the video version, if you prefer it:
Checking only one condition in a conditional is not the only thing we can do. We can check multiple conditions, using elif
. Here is how to use elif
:
#!/bin/bash
if [ "$1" = ‘Hello’ ]
then
echo 'Hello back to you!'
elif [ "$1" = ‘Hi’ ]
then
echo 'Hi!'
else
echo 'You are rude.'
fi
This is our entire shell script with an added elif
, modeled after (Ward, 2014). Let’s look at its output:
mislav@mislavovo-racunalo:~/Linux_folder$ ./tutorialScript.sh Hello
Hello back to you!
mislav@mislavovo-racunalo:~/Linux_folder$ ./tutorialScript.sh Hi
Hi!
mislav@mislavovo-racunalo:~/Linux_folder$ ./tutorialScript.sh
You are rude.
You can use elif
to test for multiple conditions. But there is also another construct, called case, which is more appropriate than just a bunch of elif
s. We will talk about case later on.
Hope you learned something new!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258