Case statements on Linux

The case statement in the bash shell provides an interesting and easy alternative to the more complex if statements. They represent the simplest form of the kind of logic that evaluates multiple values (e.g., “If it equals this, then do this. Otherwise, if it equals …). To see how if statements and case statements compare, take a look at the bash script code below that tests a numeric value.

#!/bin/bash

echo -n "Enter a whole number: "
read num

if [ $num -gt 100 ]
then
    echo "That number is greater than 100"
elif [ $num -lt 100 ]
then
    echo "That number is less than 100"
else
    echo "That number is exactly 100"
fi

That’s a fairly modest example. The variety of if statements that contain elif (else if) can go on for many more lines as in this script.

#!/bin/bash

DayOfWeek=`date +%A`

if [[ $DayOfWeek == "Monday" ]]; then
    echo "prep weekly agenda"
elif [[ $DayOfWeek == "Tuesday"  ]]; then
    echo "distribute meeting notes from last week"
elif [[ $DayOfWeek == "Wednesday"  ]]; then
    echo "check on supplies"
elif [[ $DayOfWeek == "Thursday"  ]]; then
    echo "meet with boss"
elif [[ $DayOfWeek == "Friday"  ]]; then
    echo "submit weekly report"
elif [[ $DayOfWeek == "Saturday"  ]]; then
    echo "hang out with friends"
elif [[ $DayOfWeek == "Sunday"  ]]; then
    echo "take a break"
fi

You can, however, do the same thing with case statements and they would be considerably easier – both to type and to read. Here’s the case statement version of the first if-then script shown above. Note that the line starting with *) is the equivalent of an “else”. If the value being tested doesn’t match any of those spelled out in the command, this is the command that will be run – the “default” command.

#!/bin/bash

echo -n "Enter a whole number: "
read num

case $num in
    100)  echo "$num is exactly 100";;
    [0-9][0-9]) echo "$num is less than 100";;
    *)    echo "$num is less than 100";;
esac

Here’s another example. Note that this script includes two case statements and that both include numeric ranges to demonstrate how this is done.

#!/bin/bash

echo -n "Enter a whole number: "
read num

if [ $num -gt 100 ]; then
echo Number cannor exceed 100
exit
fi

case $num in
100) echo $score equals 100 ;;
9[0-9]) echo "A" ;;
8[0-9]) echo "B" ;;
7[0-9]) echo "C" ;;
6[0-9]) echo "D" ;;
*) echo "F" ;;
esac

case $num in
100)  echo "$num is exactly 100";;
[0-9][0-9]) echo "$num is less than 100";;
*)    echo "$num is less than 100";;
esac

The second script above could be replaced with this:

#!/bin/bash

DayOfWeek=`date +%A`

case $DayOfWeek in
  Monday) echo "prep weekly agenda";;
  Tuesday) echo "distribute meeting notes from last week";;
  Wednesday) echo "check on supplies";;
  Thursday) echo "meet with boss";;
  Friday) echo "submit weekly report";;
  Saturday) echo "hang out with friends";;
  Sunday) echo "take a break";;
esac

Note that the ;; (double semicolons) at the end of each line in case statements are terminators and are necessary, but for a good reason. You could include multiple commands for each tested value, separating them with linefeeds or semicolons. The final two semicolons clearly mark the end of the commands to be run. Here are a couple syntax examples that would work identically:



Source link