Shell Programming: Looping
Commands (for, while, until)
Presented by: [Your Name]
Introduction to Shell Programming
• - Shell: Command-line interpreter
• - Common shells: Bash, Zsh, Sh
• - Scripting automates tasks
• - Focus: Looping structures
Why Use Loops in Shell Scripts?
• - Automate repetitive tasks
• - Reduce redundancy
• - Improve efficiency
Types of Loops in Shell
• 1. for loop
• 2. while loop
• 3. until loop
for Loop - Syntax
• for variable in list
• do
• commands
• done
• Iterates over list items.
for Loop - Example
• for i in 1 2 3 4 5
• do
• echo "Number: $i"
• done
• Output:
• Number: 1
• ...
• Number: 5
while Loop - Syntax
• while [ condition ]
• do
• commands
• done
• Executes while condition is true.
while Loop - Example
• count=1
• while [ $count -le 5 ]
• do
• echo "Count is $count"
• count=$((count + 1))
• done
until Loop - Syntax
• until [ condition ]
• do
• commands
• done
• Executes until condition is true.
until Loop - Example
• x=1
• until [ $x -gt 5 ]
• do
• echo "x is $x"
• x=$((x + 1))
• done
Loop Control Commands
• - break: Exit loop
• - continue: Skip iteration
• Example:
• for i in {1..5}
• do
• if [ $i -eq 3 ]; then continue; fi
• echo $i
• done
Practical Use Cases
• - Backups with for
• - File reading with while
• - Waiting with until
Summary
• - Loops reduce redundancy
• - for: known sequences
• - while: true condition
• - until: until true condition
Questions & Answers
• “Any questions about loop usage or shell
scripting?”