Module 4: Loops (for, while, until)
Loops allow you to execute commands repeatedly until a condition is met.
In Bash scripting, the main loops are for, while, and until.
Understanding these helps you automate repetitive tasks effectively.
✅ The for Loop
- Used to iterate over a list of values, files, or ranges.
- Syntax:
for variable in list; do ...; done
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Number: $i"
done
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
✅ For Loop with Range
#!/bin/bash
for i in {1..5}
do
echo "Counting: $i"
done
✅ For Loop with Files
#!/bin/bash
for file in *.sh
do
echo "Script file: $file"
done
✅ The while Loop
- Executes commands as long as a condition is true.
- Good for reading input or waiting for events.
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
✅ Reading a File with While Loop
#!/bin/bash
while read line
do
echo "Line: $line"
done < sample.txt
✅ The until Loop
- Similar to while loop, but runs until the condition becomes true.
- Syntax:
until [ condition ]; do ...; done
#!/bin/bash
num=1
until [ $num -gt 5 ]
do
echo "Number: $num"
((num++))
done
✅ Loop Control (break & continue)
break→ exit the loop immediately.continue→ skip current iteration and move to next.
#!/bin/bash
for i in {1..10}
do
if [ $i -eq 5 ]; then
continue
fi
if [ $i -eq 8 ]; then
break
fi
echo "Value: $i"
done
✅ Summary
forloops iterate over lists, ranges, and files.whileloops run as long as a condition is true.untilloops run until a condition becomes true.breakandcontinuegive fine control over loops.
✅ By the end of this module, you can use loops to repeat tasks,
process files, and control execution flow efficiently.
Module 4: Bash Loop Assignments
These assignments help practice for, while, and until loops, including loop control with break and continue. They range from simple to medium-level complexity.
✅ Assignment 1: Print Multiples of a Number
Use a for loop to print all multiples of 3 between 1 and 50, skipping others.
for i in {1..50}
do
if (( i % 3 != 0 )); then
continue
fi
echo "Multiple of 3: $i"
done
✅ Assignment 2: Sum of Numbers in a Range
Calculate the sum of numbers from 1 to 100 using a for loop.
sum=0
for i in {1..100}
do
sum=$((sum + i))
done
echo "Sum of 1 to 100 is: $sum"
✅ Assignment 3: File Count and List
List all .txt files in a directory and count them.
count=0
for file in *.txt
do
if [ -f "$file" ]; then
echo "File: $file"
((count++))
fi
done
echo "Total .txt files: $count"
✅ Assignment 4: Reverse Counting
Count down from 10 to 1 using a while loop.
count=10
while [ $count -ge 1 ]
do
echo "Count: $count"
((count--))
done
echo "Countdown Complete!"
✅ Assignment 5: Read and Number a File
Read students.txt and print each name with a line number.
line_no=1
while read student
do
echo "$line_no. $student"
((line_no++))
done < students.txt
✅ Assignment 6: Login Attempt Simulation
Use an until loop to simulate a login system that allows 3 attempts.
attempts=0
until [ $attempts -ge 3 ]
do
read -p "Enter password: " pass
if [ "$pass" == "secret123" ]; then
echo "Login Successful"
break
else
echo "Incorrect password"
fi
((attempts++))
done
✅ Assignment 7: Print Odd Numbers Only
Use a for loop to print odd numbers between 1 and 20.
for i in {1..20}
do
if (( i % 2 == 0 )); then
continue
fi
echo "Odd Number: $i"
done
✅ Assignment 8: Infinite Loop with Exit Condition
Use a while true loop to repeatedly ask for a number until the user enters 0.
while true
do
read -p "Enter a number (0 to exit): " num
if [ $num -eq 0 ]; then
echo "Exiting loop."
break
fi
echo "You entered: $num"
done
✅ Assignment 9: Nested Loops Table
Use nested for loops to print a multiplication table from 1 to 5.
for i in {1..5}
do
for j in {1..5}
do
echo -n "$((i*j)) "
done
echo
done
✅ Assignment 10: Skip Specific Numbers
Use a for loop from 1 to 15, skip numbers 5, 10 using continue, and stop the loop if the number reaches 13 using break.
for i in {1..15}
do
if [ $i -eq 5 ] || [ $i -eq 10 ]; then
continue
fi
if [ $i -eq 13 ]; then
echo "Reached 13, exiting loop."
break
fi
echo "Value: $i"
done
