Linux Basic — Menu-Driven Scripts Practice

less than 1 minute read

Practice Exercises — Menu-Driven Scripts

Build small CLI tools that prefer menus to flags.


Warm-up

  1. Build a select-based menu to choose between date, uptime, and whoami.
  2. Add a Quit option that cleans up and exits.

Dynamic

  1. Generate a menu from /etc/init.d/ scripts and allow starting/stopping a selected service.
  2. Create a menu from plugin files in /opt/plugins and run the chosen plugin.

Styling

  1. Add color to successful actions and a --no-color flag to disable it.
  2. Ensure colors are only used when stdout is a terminal.

Robustness

  1. Trap SIGINT and SIGTERM to run a cleanup function and restore terminal settings.
  2. Save the last selected menu option to ~/.menu_state and restore it on next run.

Verification Checklist

  • Menus handle invalid numeric selections gracefully
  • --no-color disables colors and NO_COLOR=1 is respected
  • Cleanup runs on Ctrl-C and on normal exit
  • Dynamic menus handle empty directories without crashing

Next chapter → Intro to Automation

Updated: