#!/bin/bash
echo -n "enter the variety of equal sides that the form has> "
learn numsides
# make sure that reply is numeric
re="^[0-9]+$"
if ! [[ $numsides =~ $re ]] ; then
echo "error: Not a quantity" >&2; exit 1
fi
if [ $numsides -lt 3 ]; then
echo "Sorry, however that might NOT be a geometrical form"
elif [ $numsides == 3 ]; then
echo triangle
elif [ $numsides == 4 ]; then
echo sq. 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 higher ask Google"
fi
Utilizing case statements, however, makes your code a lot easier to place collectively and far simpler to learn.
#!/bin/bash
echo -n "enter the variety of equal sides that the form has> "
learn numsides
# make sure that reply is numeric
re="^[0-9]+$"
if ! [[ $numsides =~ $re ]] ; then
echo "error: Not a quantity" >&2; exit 1
fi
case "$numsides" in
0-2) echo "Sorry, however that might NOT be a geometrical form";;
3) echo triangle;;
4) echo sq. 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 higher ask Google"
esac
Every section within the case assertion should finish with two semicolons. Nonetheless, you would use a number of instructions separated by semicolons and solely use the double semicolons on the finish od that section.
Wrap-up
Discover how a lot simpler case instructions are to arrange and preserve. You’ll be able to present any variety of attainable values at the price of one line every.
