Larger Script Organization
Main Dispatch
Route a Command
A larger script can keep argument routing separate from the functions that do real work. The dispatch table maps a command to a handler.
Program
Play the script to choose the command and see the selected handler.
main_dispatch_plan.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
command="build"
case "$command" in
build) handler="run_build" ;;
clean) handler="run_clean" ;;
esac
echo "$command -> $handler"
#!/usr/bin/env bash
command="clean"
case "$command" in
build) handler="run_build" ;;
clean) handler="run_clean" ;;
esac
echo "$command -> $handler"
command ← build
3command="build"4case "$command" invalues this stepbuildcommandcase "$command" in
3command="build"4case "$command" in5 build) handler="run_build" ;;values this stepbuildcommandhandler ← run_build
4case "$command" in5 build) handler="run_build" ;;6 clean) handler="run_clean" ;;values this steprun_buildhandlerecho "$command -> $handler"
7esac8echo "$command -> $handler"outputbuild -> run_buildvalues this stepbuildcommandrun_buildhandler
command ← clean
3command="clean"4case "$command" invalues this stepcleancommandcase "$command" in
3command="clean"4case "$command" in5 build) handler="run_build" ;;values this stepcleancommandhandler ← run_clean
5 build) handler="run_build" ;;6 clean) handler="run_clean" ;;7esacvalues this steprun_cleanhandlerecho "$command -> $handler"
7esac8echo "$command -> $handler"outputclean -> run_cleanvalues this stepcleancommandrun_cleanhandler
dispatch
Dispatch selects the handler for a named command.
handler
A handler function contains the command-specific work.
main function
A main function can parse, dispatch, and report without mixing every detail together.