Conditional statements are fundamental in any programming language, including Bash. They allow you to make decisions in your scripts based on certain conditions. In this section, we will cover the basics of conditional statements in Bash, including if, else, elif, and case statements.

Key Concepts

  1. if Statement: Executes a block of code if a specified condition is true.
  2. else Statement: Executes a block of code if the condition in the if statement is false.
  3. elif Statement: Stands for "else if" and allows you to check multiple conditions.
  4. case Statement: A multi-way branch statement that is useful for handling multiple conditions.

if Statement

The if statement is used to test a condition. If the condition is true, the block of code inside the if statement is executed.

Syntax

if [ condition ]; then
    # code to execute if condition is true
fi

Example

#!/bin/bash

number=10

if [ $number -gt 5 ]; then
    echo "The number is greater than 5"
fi

Explanation

  • number=10: Assigns the value 10 to the variable number.
  • [ $number -gt 5 ]: Checks if number is greater than 5.
  • echo "The number is greater than 5": Prints the message if the condition is true.

else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

Syntax

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

Example

#!/bin/bash

number=3

if [ $number -gt 5 ]; then
    echo "The number is greater than 5"
else
    echo "The number is not greater than 5"
fi

Explanation

  • number=3: Assigns the value 3 to the variable number.
  • [ $number -gt 5 ]: Checks if number is greater than 5.
  • echo "The number is not greater than 5": Prints the message if the condition is false.

elif Statement

The elif statement allows you to check multiple conditions.

Syntax

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

Example

#!/bin/bash

number=7

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

Explanation

  • number=7: Assigns the value 7 to the variable number.
  • [ $number -gt 10 ]: Checks if number is greater than 10.
  • [ $number -gt 5 ]: Checks if number is greater than 5 but less than or equal to 10.
  • echo "The number is greater than 5 but less than or equal to 10": Prints the message if the second condition is true.

case Statement

The case statement is used to simplify complex conditional statements. It is similar to the switch statement in other programming languages.

Syntax

case expression in
    pattern1)
        # code to execute if expression matches pattern1
        ;;
    pattern2)
        # code to execute if expression matches pattern2
        ;;
    *)
        # code to execute if expression does not match any pattern
        ;;
esac

Example

#!/bin/bash

day="Monday"

case $day in
    "Monday")
        echo "Start of the work week"
        ;;
    "Friday")
        echo "End of the work week"
        ;;
    "Saturday" | "Sunday")
        echo "Weekend!"
        ;;
    *)
        echo "Midweek day"
        ;;
esac

Explanation

  • day="Monday": Assigns the value "Monday" to the variable day.
  • case $day in: Starts the case statement, checking the value of day.
  • "Monday"): Matches the value "Monday".
  • echo "Start of the work week": Prints the message if the value is "Monday".
  • "Saturday" | "Sunday"): Matches either "Saturday" or "Sunday".
  • echo "Weekend!": Prints the message if the value is "Saturday" or "Sunday".

Practical Exercises

Exercise 1: Simple if Statement

Write a script that checks if a number is positive.

#!/bin/bash

number=-5

if [ $number -gt 0 ]; then
    echo "The number is positive"
fi

Solution

#!/bin/bash

number=-5

if [ $number -gt 0 ]; then
    echo "The number is positive"
else
    echo "The number is not positive"
fi

Exercise 2: Using elif

Write a script that categorizes a number as positive, negative, or zero.

#!/bin/bash

number=0

if [ $number -gt 0 ]; then
    echo "The number is positive"
elif [ $number -lt 0 ]; then
    echo "The number is negative"
else
    echo "The number is zero"
fi

Solution

#!/bin/bash

number=0

if [ $number -gt 0 ]; then
    echo "The number is positive"
elif [ $number -lt 0 ]; then
    echo "The number is negative"
else
    echo "The number is zero"
fi

Exercise 3: case Statement

Write a script that prints a message based on the day of the week.

#!/bin/bash

day="Wednesday"

case $day in
    "Monday")
        echo "Start of the work week"
        ;;
    "Friday")
        echo "End of the work week"
        ;;
    "Saturday" | "Sunday")
        echo "Weekend!"
        ;;
    *)
        echo "Midweek day"
        ;;
esac

Solution

#!/bin/bash

day="Wednesday"

case $day in
    "Monday")
        echo "Start of the work week"
        ;;
    "Friday")
        echo "End of the work week"
        ;;
    "Saturday" | "Sunday")
        echo "Weekend!"
        ;;
    *)
        echo "Midweek day"
        ;;
esac

Common Mistakes and Tips

  • Using = instead of -eq for numeric comparison: In Bash, = is used for string comparison, while -eq is used for numeric comparison.
  • Forgetting to close if statements with fi: Always ensure that your if statements are properly closed with fi.
  • Incorrect syntax in case statements: Ensure that each pattern ends with ) and each block ends with ;;.

Conclusion

In this section, we covered the basics of conditional statements in Bash, including if, else, elif, and case statements. These constructs are essential for making decisions in your scripts and handling different conditions effectively. Practice the exercises provided to reinforce your understanding and prepare for more advanced scripting techniques.

© Copyright 2024. All rights reserved