Shell scripts use control statements like conditionals (if, case) and loops (for, while, until) to control the flow of execution based on conditions or repetitive tasks. Here’s an overview and examples of how to use these control statements in shell scripts.
1. Conditional Statements
1.1 if-else Statement
Executes commands based on whether a condition is true or false.
Syntax:
if [ condition ]; then
commands
else
commands
fi
Example: Check Even or Odd Number
#!/bin/bash
echo “Enter a number:”
read num
if ((num % 2 == 0)); then
echo “$num is even.”
else
echo “$num is odd.”
fi
1.2 elif Ladder
Used for multiple conditions.
Syntax:
if [ condition1 ]; then
commands
elif [ condition2 ]; then
commands
else
commands
fi
Example: Grade Evaluation
#!/bin/bash
echo “Enter your marks:”
read marks
if [ $marks -ge 90 ]; then
echo “Grade: A”
elif [ $marks -ge 75 ]; then
echo “Grade: B”
elif [ $marks -ge 50 ]; then
echo “Grade: C”
else
echo “Grade: F”
fi
1.3 case Statement
Simplifies multiple conditional checks.
Syntax:
case value in
pattern1)
commands ;;
pattern2)
commands ;;
*)
commands ;; # Default case
esac
Example: Day of the Week
#!/bin/bash
echo “Enter a number (1-7):”
read day
case $day in
1) echo “Monday” ;;
2) echo “Tuesday” ;;
3) echo “Wednesday” ;;
4) echo “Thursday” ;;
5) echo “Friday” ;;
6) echo “Saturday” ;;
7) echo “Sunday” ;;
*) echo “Invalid input!” ;;
esac
2. Looping Statements
2.1 for Loop
Iterates over a list of items.
Syntax:
for variable in list; do
commands
done
Example: Print Numbers
#!/bin/bash
for num in {1..5}; do
echo $num
done
2.2 while Loop
Executes commands while a condition is true.
Syntax:
while [ condition ]; do
commands
done
Example: Countdown
#!/bin/bash
count=5
while [ $count -gt 0 ]; do
echo $count
((count–))
done
2.3 until Loop
Executes commands until a condition becomes true.
Syntax:
until [ condition ]; do
commands
done
Example: Count to 5
#!/bin/bash
count=1
until [ $count -gt 5 ]; do
echo $count
((count++))
done
2.4 Nested Loops
Loops can be nested for complex operations.
Example: Multiplication Table
#!/bin/bash
for i in {1..5}; do
for j in {1..5}; do
echo -n “$((i * j)) “
done
echo
done
3. Break and Continue
3.1 break Statement
Exits the loop prematurely.
Example: Exit on Condition
#!/bin/bash
for num in {1..10}; do
if [ $num -eq 5 ]; then
break
fi
echo $num
done
3.2 continue Statement
Skips the current iteration and moves to the next.
Example: Skip Even Numbers
#!/bin/bash
for num in {1..10}; do
if ((num % 2 == 0)); then
continue
fi
echo $num
done
4. Combining Control Statements
Control statements can be combined for complex logic.
Example: Menu-driven Script
#!/bin/bash
while true; do
echo “1. Show Date”
echo “2. Show Current Directory”
echo “3. Exit”
echo “Choose an option:”
read choice
case $choice in
1) date ;;
2) pwd ;;
3) echo “Exiting…”; break ;;
*) echo “Invalid choice!” ;;
esac
done
Tips for Writing Control Statements
- Use Indentation: Improves readability.
- Test Conditions: Use -eq, -ne, -lt, -le, -gt, -ge for numeric comparisons and ==, != for strings.
- Debugging: Use set -x to debug scripts.
- Quoting Variables: Always quote variables to avoid issues with spaces.
By mastering control statements, you can create dynamic and interactive shell scripts!