Shell
Programming
Commands
Understanding Control Flow with For, While, and
Until Loops using Bash Commands
Introduction
This presentation explores
key conditional statements in
shell programming: For,
While, and Until loops with
one example for each
conditional statements.
01
For Loop Shell
Commands
Syntax of For Loop
For loops are useful for tasks such as iterating
through files in a directory, processing list items,
or executing repetitive tasks where the number of
iterations is known beforehand.
The basic syntax is:
for (( initialization; condition; increment ))
do
# commands
done
Example of For Loop
for (( i=1; i<=5; i++ ))
do
echo “Iteration: $i”
done
02
While Loop Shell
Commands
Syntax of While Loop
The While loop in shell scripting continues executing commands as long as a
specified
→ condition is true.
While loops are particularly helpful when the number of iterations is not known
beforehand, such as monitoring system processes, waiting for user input, or
executing commands until a specific condition is met.
The syntax is:
Loop variable Initialization
while [ condition ]
do
→
# commands
# Updation of loop variable
done
Example of While Loop
03
Until Loop Shell
Commands
Syntax of Until Loop
An Until loop executes commands until a specified condition becomes true.
Until loops are beneficial for situations where you want to ensure something is done until a
given condition is satisfied, like waiting for a file to appear or running a task until successful.
The syntax is:
until [CONDITION]
do
# Commands to execute while condition is false
done
This means the loop will continue executing as long as the condition evaluates to false.
Example of Until Loop
counter=5
until [ $counter -lt 0 ]
do
echo "Countdown: $counter“
((counter--))
Done
echo "Time's up!"
Conclusions
In summary, For, While, and Until loops are
essential control flow structures in shell
programming. Each has unique applications that
allow for effective automation and scripting.
Do you have any questions?
Thank you!
www.yourwebsite.com ↓
+
9
1
6
2
0
4
2
1
8
3
8