loading...
Cover image for Shell Scripting for Newbies 03

Shell Scripting for Newbies 03

bardiniaz profile image Augusto Zanella Bardini ・3 min read

Conditionals

In the last tutorial, you've given a look at how to interact with the user and how to return simple outputs accordingly to the user interaction.

Ironically, your cat's been using the computer lately to ask for things. Of course it's not working, but you can solve this problem! Lets talk to your cat!

First of all, we'll want to ask him what does he want. Let's do it this way:

#/bin/bash

printf "What do you want, fella?\n"
read answer
  • TIP: printf is a better alternative to the command echo. By now, it's just the same, except from you'll need to write \n in the end of the sentences in order to go to the next line.

Now that you've got his answer in the variable answer, we'll see this NEW hell no AWESOME logic: The if command. It works almost as simple as it is written, but with some caveats. We already know that it's possible to test the truth of a statement with the command test, so let's just tell the computer what to do if it's true:

#/bin/bash

printf "What do you want, fella?\n"
read answer

#if the test is true, then it'll be printed the message for the cat
if test $answer = 'food'
then printf "There's meat in the fridge, dumbass!\n"
fi 

Here we've literally responded to the cat to find his own food! To tell the computer where the if conditional ends, just use this is not a joke the command fi.
There's a simple trick to make the code even better readable. We can exchange the command test to a pair of [ ] around the statements, have a look:

#/bin/bash

printf "What do you want, fella?\n"
read answer

#if the test is true, then it'll be printed the message for the cat
if [ $answer = 'food' ]
then printf "There's meat in the fridge, dumbass!\n"
fi

Cool, but your lil' kitty might not really know what he wants. So let's just add another literal command, say hello to the else command! In case his answer is anything but food, let's just ignore it poor cat and give him the cuddling option.

#/bin/bash

printf "What do you want, fella?\n"
read answer

#if the test is true, then it'll be printed the message for the cat
if [ $answer = "food" ]
then 
        printf "There's meat in the fridge, dumbass!\n"
else
        #now if his last answer was anything but food, you'll make this question 
        printf "Want some cuddle, so? [yes/no]\n"
        read answer
        #if his answer is yes again, you tell him you are busy,
        #else, you tell him to top annoying!
        if [ $answer = "yes" ]
        then
                printf "Sorry, I'm busy learning shell scripting.\n"
        else
                printf "So stop annoying me!\n"

        fi 
fi
  • TIP: Learning how to use the if command is actually intuitive. The boring funny part is understanding completely the test command. You can have a deeper look at bash expressions and when you should use them clicking here.

Now this is getting bigger! Once the cat's answer is anything but food in the first question, we give him another question with please stop
two more options! Notice that this question is only made if the first answer IS NOT food. The possible outputs of our script are shown below.

What do you want, fella?
food
There's meat in the fridge, dumbass!
What do you want, fella?
idk
Want some cuddle, so? [yes/no]
yes
Sorry, I'm busy learning shell scripting.
What do you want, fella?
communism
Want some cuddle, so? [yes/no]
no
So stop annoying me!

About

You can find all my scripts in my github!

Follow me for more articles!

Posted on by:

bardiniaz profile

Augusto Zanella Bardini

@bardiniaz

I'm a student trying a new way to learn things better: writing it down. As I think it's paper wasting to write in a notebook, I've created this profile to post my experiences.

Discussion

markdown guide