Linux Basic — Menu-Driven Scripts Practice
Practice Exercises — Menu-Driven Scripts
Build small CLI tools that prefer menus to flags.
Warm-up
- Build a
select-based menu to choose betweendate,uptime, andwhoami. - Add a
Quitoption that cleans up and exits.
Dynamic
- Generate a menu from
/etc/init.d/scripts and allow starting/stopping a selected service. - Create a menu from plugin files in
/opt/pluginsand run the chosen plugin.
Styling
- Add color to successful actions and a
--no-colorflag to disable it. - Ensure colors are only used when stdout is a terminal.
Robustness
- Trap
SIGINTandSIGTERMto run a cleanup function and restore terminal settings. - Save the last selected menu option to
~/.menu_stateand restore it on next run.
Verification Checklist
- Menus handle invalid numeric selections gracefully
--no-colordisables colors andNO_COLOR=1is respected- Cleanup runs on Ctrl-C and on normal exit
- Dynamic menus handle empty directories without crashing
Next chapter → Intro to Automation