Interview Prep/Shell Scripting/Scenarios: writing and hardening scripts

You need a deploy script that humans and CI both call with `--env`, `--version` and `--dry-run`. How do you design its interface?

BeginnerScenarioScenarios: writing and hardening scripts
Answer

Situation read → A good CLI is predictable for humans and strict for machines: clear flags, validation, meaningful exit codes, no prompts in CI.

What I'd do →

  1. Parse with a while (($#)); case $1 in loop to support long options, and reject unknown flags with exit 2.
  2. Validate: [[ $env =~ ^(dev|staging|prod)$ ]], [[ $version =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]].
  3. Support env vars as defaults for CI (env=${DEPLOY_ENV:-}), with flags overriding them.
  4. Prompt for confirmation on prod only when [[ -t 0 ]]; in CI require an explicit --yes.
  5. Implement --dry-run through a run() wrapper that prints commands with printf '%q ' instead of executing them.
  6. Document exit codes in --help (0 ok, 1 failed, 2 usage, 3 health check failed).

Why → CI pipelines branch on exit codes, and humans need --help and dry runs to trust a script.

Interview tip → Say "the script never prompts when there is no terminal", which shows you have been burned by a hung CI job.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: writing and hardening scripts