Control Structures in Bash: Conditionals and Loops

October 27, 2024

Control Structures in Bash: Conditionals and Loops

This lesson focuses on control structures like if, for, and while loops. Readers will learn how to implement logic in their scripts to automate tasks effectively.

Understanding Control Structures

Control structures are essential for adding logic to your Bash scripts. They allow your scripts to make decisions and repeat actions based on certain conditions. The two main types of control structures we will cover in this lesson are conditionals and loops.

Conditionals: The if Statement

The if statement is used to execute commands based on whether a condition is true or false. Here’s the basic syntax:

if [ condition ]; then
    # commands to execute if condition is true
else
    # commands to execute if condition is false
fi

Let’s look at an example:

#!/bin/bash

read -p "Enter your age: " age

if [ $age -ge 18 ]; then
    echo "You are an adult."
else
    echo "You are a minor."
fi

In this script, we prompt the user to enter their age. If the age is 18 or older, the script will output that the user is an adult; otherwise, it will indicate that the user is a minor.

Using elif for Multiple Conditions

Sometimes, you may need to check multiple conditions. In such cases, you can use elif (else if) to handle additional conditions:

if [ condition1 ]; then
    # commands for condition1
elif [ condition2 ]; then
    # commands for condition2
else
    # commands if none of the above conditions are true
fi

Here’s an example:

#!/bin/bash

read -p "Enter your score: " score

if [ $score -ge 90 ]; then
    echo "Grade: A"
elif [ $score -ge 80 ]; then
    echo "Grade: B"
elif [ $score -ge 70 ]; then
    echo "Grade: C"
else
    echo "Grade: D"
fi

This script evaluates the user’s score and assigns a grade based on the value.

Loops: The for Loop

The for loop is used to iterate over a list of items. Here’s the basic syntax:

for item in list; do
    # commands to execute for each item
done

For example:

#!/bin/bash

for number in {1..5}; do
    echo "Number: $number"
done

This script will print the numbers from 1 to 5.

The while Loop

The while loop continues to execute as long as a specified condition is true. The basic syntax is:

while [ condition ]; do
    # commands to execute while condition is true
done

Here’s an example:

#!/bin/bash

count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    ((count++))
done

This script will print the count from 1 to 5, incrementing the count on each iteration.

Nesting Control Structures

You can also nest control structures within each other. For example:

#!/bin/bash

for i in {1..3}; do
    for j in {1..2}; do
        echo "i: $i, j: $j"
    done
done

This script uses a nested loop to print the values of i and j.

Conclusion

Control structures are a powerful feature in Bash scripting that allow you to implement logic and automate tasks effectively. By mastering if statements and loops, you can create more complex and functional scripts. Practice using these structures in your scripts to see how they can improve your coding capabilities!