Pattern: Missing default case
Issue: -
Found case
statement may not be considering all possible cases. This may mean that only the happy paths are accounted for. Consider adding a default case to handle other values. If you don't know what to do or don't believe it'll ever happen, exiting with an error is good, fail-fast practice.
The example is adapted from a real world Debian init script, which due to a missing default case reports success on any misspelled command (here with underscore instead of dash):
$ /etc/init.d/screen-cleanup force_reload && echo success
success
Example of incorrect code:
case "$1" in
start) start_service ;;
stop) stop_service ;;
restart|reload|force-reload)
stop_service;
start_service;;
esac
Example of correct code:
case "$1" in
start) start_service ;;
stop) stop_service ;;
restart|reload|force-reload)
stop_service;
start_service;;
*)
echo >&2 "Invalid choice: $1"
exit 1
esac