Pattern: Unreachable command
Issue: -
The problematic code wanted to exit with success if the user explicitly asked for --help
. However, since the usage
function already had an exit 1
, this statement could never run.
One possible solution is to change usage()
to only echo, and let callers be responsible for exiting.
Example of incorrect code:
usage() {
echo >&2 "Usage: $0 -i input"
exit 1
}
if [ "$1" = "--help" ]
then
usage
exit 0 # Unreachable
fi
Example of correct code:
usage() {
echo >&2 "Usage: $0 -i input"
}
if [ "$1" = "--help" ]
then
usage
exit 0
fi