Control structures are fundamental components in shell scripting that allow you to control the flow of your script based on conditions and loops. In this section, we will cover the following key concepts:

  1. Conditional Statements
    • if statements
    • case statements
  2. Looping Constructs
    • for loops
    • while loops
    • until loops
  3. Break and Continue

  1. Conditional Statements

if Statements

The if statement is used to execute a block of code only if a specified condition is true.

Syntax:

if [ condition ]; then
    # code to execute if condition is true
elif [ another_condition ]; then
    # code to execute if another_condition is true
else
    # code to execute if none of the conditions are true
fi

Example:

#!/bin/bash

echo "Enter a number:"
read number

if [ $number -gt 10 ]; then
    echo "The number is greater than 10."
elif [ $number -eq 10 ]; then
    echo "The number is equal to 10."
else
    echo "The number is less than 10."
fi

case Statements

The case statement is used to simplify complex if-elif-else chains. It matches a variable against a set of patterns and executes the corresponding block of code.

Syntax:

case $variable in
    pattern1)
        # code to execute if variable matches pattern1
        ;;
    pattern2)
        # code to execute if variable matches pattern2
        ;;
    *)
        # code to execute if variable doesn't match any pattern
        ;;
esac

Example:

#!/bin/bash

echo "Enter a letter:"
read letter

case $letter in
    [a-z])
        echo "You entered a lowercase letter."
        ;;
    [A-Z])
        echo "You entered an uppercase letter."
        ;;
    *)
        echo "You entered a non-alphabetic character."
        ;;
esac

  1. Looping Constructs

for Loops

The for loop is used to iterate over a list of items and execute a block of code for each item.

Syntax:

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

Example:

#!/bin/bash

for i in 1 2 3 4 5; do
    echo "Number: $i"
done

while Loops

The while loop executes a block of code as long as a specified condition is true.

Syntax:

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

Example:

#!/bin/bash

count=1

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

until Loops

The until loop is similar to the while loop, but it executes a block of code as long as a specified condition is false.

Syntax:

until [ condition ]; do
    # code to execute until condition is true
done

Example:

#!/bin/bash

count=1

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

  1. Break and Continue

break

The break statement is used to exit from a loop prematurely.

Example:

#!/bin/bash

for i in 1 2 3 4 5; do
    if [ $i -eq 3 ]; then
        break
    fi
    echo "Number: $i"
done

continue

The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.

Example:

#!/bin/bash

for i in 1 2 3 4 5; do
    if [ $i -eq 3 ]; then
        continue
    fi
    echo "Number: $i"
done

Practical Exercises

Exercise 1: Basic if Statement

Write a script that asks the user for their age and prints whether they are a minor, an adult, or a senior.

Solution:

#!/bin/bash

echo "Enter your age:"
read age

if [ $age -lt 18 ]; then
    echo "You are a minor."
elif [ $age -le 65 ]; then
    echo "You are an adult."
else
    echo "You are a senior."
fi

Exercise 2: for Loop with break

Write a script that prints numbers from 1 to 10 but stops if the number is 7.

Solution:

#!/bin/bash

for i in {1..10}; do
    if [ $i -eq 7 ]; then
        break
    fi
    echo "Number: $i"
done

Exercise 3: while Loop with continue

Write a script that prints numbers from 1 to 10 but skips the number 5.

Solution:

#!/bin/bash

count=1

while [ $count -le 10 ]; do
    if [ $count -eq 5 ]; then
        count=$((count + 1))
        continue
    fi
    echo "Number: $count"
    count=$((count + 1))
done

Conclusion

In this section, we covered the essential control structures in shell scripting, including conditional statements (if and case), looping constructs (for, while, and until), and the use of break and continue statements. These control structures are crucial for writing effective and efficient shell scripts. Practice the provided exercises to reinforce your understanding and prepare for more advanced scripting topics.

© Copyright 2024. All rights reserved