The case and select constructs are technically not loops,
since they do not iterate the execution of a code block. Like loops,
however, they direct program flow according to conditions at the top or bottom of the block.
Controlling program flow in a code block
case (in) / esac
The case construct is the shell scripting analog to switch in C/C++.
It permits branching to one of a number of code blocks, depending on condition tests.
It serves as a kind of shorthand for multiple if/then/else statements and is an appropriate tool for creating menus.
#! /bin/bash
while [ $# -gt 0 ]; do # Until you run out of parameters . . .
case "$1" in
-d|--debug)
# "-d" or "--debug" parameter?
DEBUG=1
;;
-c|--conf)
CONFFILE="$2"
shift
if [ ! -f $CONFFILE ]; then
echo "Error: Supplied file doesn't exist!"
exit $E_CONFFILE # File not found error.
fi
;;
esac
shift # Check next set of parameters.
done
# From Stefano Falsetto's "Log2Rot" script,
#+ part of his "rottlog" package.
# Used with permission