Table of Contents
- Introduction
- What Are Loops and Case Statements?
- Core Loops and Case Statements Syntax
- Real-World Scenario: Loops and Case Statements
- Conclusion
- Let's Connect
Introduction
Welcome back to Day 30 of this practical Linux challenge! Part 4/4 of our beginner shell scripting series for RHEL 9.
If you have ever wanted your Linux system to repeat tasks, check conditions, or handle input differently based on options, then you are ready to meet three of the most powerful tools in shell scripting: for
loops, do
, while
loops, and case
statements.
What Are Loops and Case Statements?
Loops help you repeat commands without having to type them repeatedly.
Case statements help you respond to different inputs, like choosing a different action depending on the value a user gives.
In Bash, we use:
- for loop: repeat something a certain number of times
- do: Starts the loop block
- while loop: repeat while something is true
- case: handle multiple possible inputs
Core Loops and Case Statements Syntax
- The
for
anddo
statement syntax;
for item in 1 2 3
do
echo "Number: $item"
done
What they do;
- for item in 1 2 3: This means we are looping through the numbers 1, 2, and 3.
- do: This begins the block of code we want to repeat.
- done: This ends the loop block. The loop knows to stop after this.
Think of do as “start the loop task” and done as “finish the loop task.
- The
while
loop statement syntax;
count=1
while [ $count -le 3 ]
do
echo "Count is $count"
((count++))
done
What they do;
- while [ $count -le 3 ]: This checks if count is 3 or less.
- do: Starts the loop block.
- ((count++)): Adds 1 to count each time.
- done: This ends the loop block. The loop knows to stop after this.
So, the loop runs until count becomes 4.
- The
case
statement syntax;
echo "Enter a fruit:"
read fruit
case $fruit in
apple)
echo "Apples are red or green.";;
banana)
echo "Bananas are yellow.";;
*)
echo "Unknown fruit.";;
esac
What they do;
- read fruit: User types a fruit name.
- case $fruit in: Begins checking value.
- apple), banana): These are the options.
- *): Wildcard — matches anything else.
- ;;: Ends each option’s command
- esac : simply means
case
spelt backward. Used to end acase
statement.
Real-World Scenario: Loops and Case Statements
Let’s write a for loop 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 forloop.sh # note that script files end with a .sh
#!/bin/bash
# FOR LOOP EXAMPLE
echo "FOR loop - Print numbers 1 to 5:"
for i in {1..5}
do
echo "Number: $i"
done
# Save and exit using:wq!
- Now, give the script permission to run
chmod +x forloop.sh # this enables it to be executable
- Verify the file has the execute permissions
ls -ltr forloop.sh
- Run the script on your terminal
./forloop.sh
- Let's practice the
while
scenario - Create a script file, paste the code, or you can write your own now, then grant it the executable permissions.
#!/bin/bash
# While loop Example
echo " WHILE loop - Count down from 3:"
count=3
while [ $count -gt 0 ]
do
echo "Countdown: $count"
count=$((count - 1))
done
# Save and exit using:wq!
- Let's practice the
case
scenario - Create a script file, paste the code, or you can write your own now, then grant it the executable permissions.
#!/bin/bash
echo "CASE statement - Choose an option:"
echo "Enter a fruit: apple, banana, or mango"
read fruit
case $fruit in
apple)
echo "You chose Apple"
;;
banana)
echo "You chose Banana"
;;
mango)
echo "You chose Mango"
;;
*)
echo "Unknown fruit. Please pick from apple, banana, or mango."
;;
esac
# Save and exit using:wq!
Conclusion
And that’s it!
You have now learned how to use for
, do
, while
, and case
statements in your shell scripts. These tools help you make your scripts smarter, more flexible, and powerful.
Take your time to practice each one, experiment on your terminal, and slowly build confidence. The more you use them, the easier it becomes.
Keep going, you are doing great!
If this is helpful to you, feel free to bookmark, comment, like and follow me!
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)