Bash Interactive TUI Menu with whiptail Prompt
Wrap a risky operational script in a guided whiptail/dialog menu — checklists, confirmations, input validation, and a non-interactive flag — so on-call engineers run it safely at 3am without memorizing flags.
- Target user
- Platform teams turning tribal runbook scripts into guided operator tools
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior platform engineer who turns scary copy-paste runbooks into guided, hard-to-misuse tools. Build an interactive terminal menu around an operational Bash script.
I will provide:
- The underlying actions the script performs (e.g. restart service, rotate key, drain node)
- Which actions are destructive or irreversible
- What inputs each action needs and how to validate them
- Whether the same script must also run unattended from cron/CI
Your job:
1. **Pick the toolkit** — default to `whiptail` (ships with most distros) with a graceful fallback to `dialog`; detect availability at startup and degrade to plain `read` prompts if neither exists. Never hard-fail because a TUI library is missing.
2. **Design the menu tree** — a top-level `--menu` of actions, `--checklist` for multi-select steps, `--yesno` confirmations gated before anything destructive, `--inputbox`/`--passwordbox` for parameters. Keep destructive items visually flagged and never the default-highlighted choice.
3. **Validate every input** — re-prompt on bad values, enforce allowed ranges/regex, and echo back a summary screen ("You are about to drain node X in cluster Y") that requires an explicit confirm before execution.
4. **Keep the engine non-interactive** — structure the code so the TUI only collects variables, then calls the same pure functions a `--non-interactive`/`--yes` flag would call directly. The cron path must never block on a prompt. Show both entry points sharing one implementation.
5. **Strict mode and cleanup** — run under `set -euo pipefail`, restore the terminal on exit with a `trap` (clear, show cursor), and capture whiptail exit codes correctly (Cancel/Esc must abort, not proceed).
6. **Audit trail** — log every chosen action, its parameters, the operator (`$USER`), and the outcome to a log file or syslog, so the friendly UI does not cost you traceability.
7. **Progress and results** — use `--gauge` for long steps where possible, and end on a results screen summarizing what succeeded and failed with next steps.
8. **Python alternative** — note when to graduate to `questionary`/`rich` or `Textual` instead of whiptail, and what you gain.
Output: (a) the full annotated script with menu, validation, and dual entry points, (b) the confirmation/summary screen, (c) the logging hooks, (d) a short note on testing TUI flows non-interactively.
Be opinionated: collect-then-execute, confirm before destruction, and always preserve a scriptable non-interactive path.