Table of Contents
- Introduction
- What is an
if
statement? - Core
if
Statement Syntax - Real-World Scenario:
if
Statement - Conclusion
- Let's Connect
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
andelif
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
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!
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?
- Now, give the script permission to run
chmod +x checkfile.sh # this enables it to be executable
- Verify the file has the execute permissions
ls -ltr checkfile.sh
- 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.
- 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!
Note;
- -gt: means greater than
- -lt: means less than
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
Top comments (0)