DEV Community

Amanda Igwe
Amanda Igwe

Posted on

3 1 1 1 1

Day 29/ 30 Days of Linux Mastery: if Statements in Shell Scripting for Beginners

Table of Contents


Introduction

Welcome back to Day 29 of this practical Linux challenge! Part 3 of our beginner shell scripting series for RHEL 9.

We are looking at the Conditional statements like the if elif then else statements.

They allow your script to make decisions based on a condition.


What is an if statement?

Have you ever made a decision based on a condition? Let's say it is the weekend, and you are planning your rest. You decide that if it rains, then you will stay indoors and watch your Netflix, else you will go out to the cinema.

Now because you really don't want to go out you add more conditions like if it rains, then you will stay indoors and watch your Netflix, elif its cloudy, then you have to cancel leaving the house, else you have no choice but go out to the cinema.

Simple logic, right? But you have just written a script with conditional statements.


Core if Statement Syntax

  • The if and elif statement syntax;
# for if statement 

if [ CONDITION ]
then
    # do something
else
    # do something else
fi


# for elif statement

if [ CONDITION ]
then
    # do something
elif [ CONDITION ]
then
    # do something again
else
    # do something else
fi
Enter fullscreen mode Exit fullscreen mode

Each part explained:

  • if [ CONDITION ]: We are checking if something is true
  • then: If it is true, do this...
  • elif [ CONDITION ]: We are checking if another condition is true
  • else: If it is not true, do this instead...
  • fi: This is just if spelt backwards, it tells bash that you're done with the condition.

Real-World Scenario: if Statement

Let’s write a conditional script that checks for a file and let us know if it exists or not.

  • Open your terminal, create a file and add this line into it
vim checkfile.sh   # note that script files end with a .sh

#!/bin/bash

if [ -f lines.txt ]; 
then
    echo "File exists."
else
    echo "File does not exist."
fi

# Save and exit using:wq!
Enter fullscreen mode Exit fullscreen mode

Flags Meaning
-f : Is it a regular file?
-d : Is it a directory?
-e : Does it exist (file or dir)?
-z : Is the string empty?

ag2 description

  • Now, give the script permission to run
chmod +x checkfile.sh     # this enables it to be executable
Enter fullscreen mode Exit fullscreen mode
  • Verify the file has the execute permissions
ls -ltr checkfile.sh
Enter fullscreen mode Exit fullscreen mode
  • Run the script on your terminal
./checkfile.sh

# Now edit the file and alter the name of the file to a file that doesn't exist.
Enter fullscreen mode Exit fullscreen mode

ag3 description

  • Let's practice the elif scenario
  • Create a script file, paste the code, or you can write your own now, then grant it the executable permissions.
#!/bin/bash

echo "Enter a number:"
read num

if [ "$num" -gt 100 ]; 
then
    echo "That's a big number!"
elif [ "$num" -gt 50 ]; 
then
    echo "That's a medium number."
else
    echo "That's a small number."
fi

# Save and exit using:wq!
Enter fullscreen mode Exit fullscreen mode

Note;

  • -gt: means greater than
  • -lt: means less than

ag5 description

ag7 description

Conclusion

Congratulations!
You just learned how to make your bash scripts smart with if, then, else, and elif. You can now write scripts that react based on conditions.

If this is helpful to you, feel free to bookmark, comment, like and follow me for Day 30!


Let's Connect!

If you want to connect or share your journey, feel free to reach out on LinkedIn.
I am always happy to learn and build with others in the tech space.

#30DaysLinuxChallenge #Redhat#RHCSA #RHCE #CloudWhistler #Linux #Rhel #Ansible #Vim #CloudComputing #DevOps #LinuxAutomation #IaC #SysAdmin#CloudEngineer

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

World's Largest Hackathon: Building Conversational Video Agents with Tavus and Bolt

With Tavus + Bolt, you can easily build real-time conversational AI apps that see, hear, and speak, just like humans. You won’t want to miss it!.

Tune in to the full event

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️