Can I use multiple 'elif' statements?

0163

Yes, you can use multiple elif statements in a Bash script. This allows you to check several different conditions sequentially. Each elif can have its own condition, and if none of the conditions in the if or elif statements are true, the script will execute the else block (if provided).

Here's an example:

#!/bin/bash

echo "Enter a number: "
read number

if [ $number -lt 0 ]; then
    echo "The number is negative."
elif [ $number -eq 0 ]; then
    echo "The number is zero."
elif [ $number -gt 0 ] && [ $number -le 10 ]; then
    echo "The number is between 1 and 10."
elif [ $number -gt 10 ] && [ $number -le 100 ]; then
    echo "The number is between 11 and 100."
else
    echo "The number is greater than 100."
fi

In this example, multiple elif statements are used to check different ranges of the input number.

0 Comments

no data
Be the first to share your comment!