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 →
- Parse with a
while (($#)); case $1 inloop to support long options, and reject unknown flags with exit 2. - Validate:
[[ $env =~ ^(dev|staging|prod)$ ]],[[ $version =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]. - Support env vars as defaults for CI (
env=${DEPLOY_ENV:-}), with flags overriding them. - Prompt for confirmation on prod only when
[[ -t 0 ]]; in CI require an explicit--yes. - Implement
--dry-runthrough arun()wrapper that prints commands withprintf '%q 'instead of executing them. - 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.
More on Scenarios: writing and hardening scripts
You inherit a 600-line `deploy.sh` with no error handling. It "sometimes half-deploys". Where do you start?IntermediateA script with `#!/bin/sh` works on CentOS but fails on Ubuntu with `Syntax error: "(" unexpected`. What is going on and what do you do?BeginnerA maintenance script must run as the `deploy` user, never as root, because files it creates must be owned by `deploy`. How do you enforce that?BeginnerTen scripts in a repo each copy-paste their own `log`, `die`, retry and config loading. How do you fix this?IntermediateA script is run both by engineers and by cron. Cron logs are full of colour codes and the script sometimes hangs waiting for input. Fix it.BeginnerYour team decides all scripts must pass ShellCheck. The first run reports 400 findings across 60 scripts. How do you roll this out?Intermediate