Site icon GIXtools

Making a case for case statements on Linux

If you’ve never used bash case statements, you might be very surprised by how easily they allow you to simplify the logic of your scripts. Instead of composing a long group of code lines that would allow you to test a variable against a number of potential values, you could write a terse and delightfully readable chunk of code that’s easy to put together. When you use the if/then option, you have quite a bit of flexibility, but you can end up having to put a lot of code together. For example, you could test a variable and take an action based on its value in a script like this:

#!/bin/bash

echo -n "enter the number of equal sides that the shape has> "
read numsides

# make sure answer is numeric
re='^[0-9]+$'
if ! [[ $numsides =~ $re ]] ; then
   echo "error: Not a number" >&2; exit 1
fi

if [ $numsides -lt 3 ]; then
    echo "Sorry, but that would NOT be a geometric shape"
fi
if [ $numsides == 3 ]; then
    echo triangle
fi
if [ $numsides == 4 ]; then
    echo square or rectangle
fi
if [ $numsides == 5 ]; then
    echo pentagon 
fi
if [ $numsides == 6 ]; then
    echo hexagon 
fi
if [ $numsides == 7 ]; then
    echo heptagon 
fi
if [ $numsides == 8 ]; then
    echo octagon 
fi
if [ $numsides == 9 ]; then
    echo nonaagon 
fi
if [ $numsides == 10 ]; then
    echo decagon 
fi
if [ $numsides == 11 ]; then
    echo hendecagon 
fi
if [ $numsides == 12 ]; then
    echo dodecagon 
fi
if [ $numsides -gt 12 ]; then
    echo "Hmm, you’d better ask Google"
fi

Notice that the script will exit with an error if the value entered isn’t numeric or is not provided.

You can also take advantage of “elif” (else if) clauses to lessen the number of lines you need. Here’s a redo of the script above that displays this option.

#!/bin/bash

echo -n "enter the number of equal sides that the shape has> "
read numsides

# make sure answer is numeric
re='^[0-9]+$'
if ! [[ $numsides =~ $re ]] ; then
   echo "error: Not a number" >&2; exit 1
fi

if [ $numsides -lt 3 ]; then
    echo "Sorry, but that would NOT be a geometric shape"
elif [ $numsides == 3 ]; then
    echo triangle
elif [ $numsides == 4 ]; then
    echo square or rectangle
elif [ $numsides == 5 ]; then
    echo pentagon 
elif [ $numsides == 6 ]; then
    echo hexagon 
elif [ $numsides == 7 ]; then
    echo heptagon 
elif [ $numsides == 8 ]; then
    echo octagon 
elif [ $numsides == 9 ]; then
    echo nonagon 
elif [ $numsides == 10 ]; then
    echo decagon 
elif [ $numsides == 11 ]; then
    echo hendecagon 
elif [ $numsides == 12 ]; then
    echo dodecagon 
elif [ $numsides -gt 12 ]; then
    echo "Hmm, you’d better ask Google"
fi

Using case statements, on the other hand, makes your code much simpler to put together and much easier to read.

#!/bin/bash

echo -n "enter the number of equal sides that the shape has> "
read numsides

# make sure answer is numeric
re='^[0-9]+$'
if ! [[ $numsides =~ $re ]] ; then
   echo "error: Not a number" >&2; exit 1
fi

case "$numsides" in
    0-2) echo "Sorry, but that would NOT be a geometric shape";;
    3) echo triangle;;
    4) echo square or rectangle;;
    5) echo pentagon;;
    6) echo hexagon;;
    7) echo heptagon;;
    8) echo octogon;;
    9) echo nonagon;;
    10) echo decagon;;
    11) echo hendecagon;;
    12) echo dodecaggon;;
    *) echo "Hmm, you’d better ask Google"
esac

Each segment in the case statement must end with two semicolons. However, you could use multiple commands separated by semicolons and only use the double semicolons at the end od that segment.

Wrap-up

Notice how much easier case commands are to set up and maintain. You can provide any number of possible values at the cost of one line each.

Source:: Network World

Exit mobile version