If we want to execute a group of commands multiple times, we should use iterative statements. There are 3 types of iterative statements in shell scripting:
whileloopuntilloopforloop
If we don't know the number of iterations in advance, we use the while loop.
while [ condition ]
do
body
doneAs long as the condition is true, the body will execute. Once the condition fails, the loop terminates.
#!/bin/bash
i=1
while [ $i -le 10 ]
do
echo $i
let i++
done#!/bin/bash
read -p "Enter Limit: " n
i=1
while [ $i -le $n ]
do
echo $i
sleep 2
let i++
doneNote: The sleep command pauses execution for the specified number of seconds. Examples:
sleep 2→ Pause for 2 secondssleep 0.5→ Pause for 0.5 seconds
#!/bin/bash
read -p "Enter n value: " n
i=1
sum=0
while [ $i -le $n ]
do
let sum=sum+i
let i++
done
echo "The Sum of first $n numbers: $sum"- Input:
3
Output:The Sum of first 3 numbers: 6 - Input:
10
Output:The Sum of first 10 numbers: 55
#!/bin/bash
while [ true ]
do
clear
printf "\n\n\n\n\n\n\t\t\t\t$(date +%H:%M:%S)"
sleep 1
doneNote:
- Use the
printfcommand instead ofechofor escape characters like\nand\t. - The
truekeyword represents an infinite loop as the condition always evaluates totrue.
sleepCommand: Pauses execution for a given time in seconds.- Escape Characters:
\n: New line\t: Tab
- Keywords:
true: Represents a boolean value that keeps the loop running indefinitely.false: Represents a boolean value that exits the loop immediately.
This script continuously checks if a server is reachable.
#!/bin/bash
while [ true ]
do
ping -c 1 example.com > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Server is up"
else
echo "Server is down"
fi
sleep 5
doneUse Case: Monitor server uptime in real-time.
This script creates backups of a directory every 10 seconds.
#!/bin/bash
source_dir="/path/to/source"
backup_dir="/path/to/backup"
while [ true ]
do
timestamp=$(date +%Y%m%d%H%M%S)
cp -r $source_dir "$backup_dir/backup_$timestamp"
echo "Backup created at $timestamp"
sleep 10
doneUse Case: Automating periodic backups.
This script monitors disk space usage and alerts when it exceeds a threshold.
#!/bin/bash
threshold=90
while [ true ]
do
usage=$(df / | grep '/' | awk '{print $5}' | sed 's/%//')
if [ $usage -gt $threshold ]; then
echo "Disk usage is above $threshold%: Current usage is $usage%"
fi
sleep 30
doneUse Case: Preventing disk space issues on servers.
This script downloads a file in chunks until the download is complete.
#!/bin/bash
file_url="https://example.com/largefile.zip"
output_file="largefile.zip"
while [ ! -f "$output_file" ]
do
wget -c $file_url -O $output_file
if [ $? -eq 0 ]; then
echo "Download complete."
break
else
echo "Retrying download..."
fi
sleep 5
doneUse Case: Handling unreliable network connections during downloads.
This script prompts for a password and allows three attempts for correct input.
#!/bin/bash
correct_password="secret123"
attempts=0
max_attempts=3
while [ $attempts -lt $max_attempts ]
do
read -sp "Enter your password: " password
echo
if [ "$password" == "$correct_password" ]; then
echo "Access granted."
break
else
echo "Incorrect password."
attempts=$((attempts + 1))
fi
done
if [ $attempts -eq $max_attempts ]; then
echo "Access denied. Too many failed attempts."
fiUse Case: Login system with retry limits.