You inherit a 600-line `deploy.sh` with no error handling. It "sometimes half-deploys". Where do you start?
IntermediateScenarioScenarios: writing and hardening scripts
Answer
Situation read → Half-deploys mean a step fails and the script keeps going. Before rewriting anything, make failures visible and stop on them.
What I'd do →
- Put it in Git if it is not already, and run
shellcheck -x deploy.shto get a list of real bugs (unquoted variables, uncheckedcd, masked return codes). - Add a strict header and an error trap so the next failure tells us where:
set -Eeuo pipefail trap 'echo "FAILED line $LINENO: $BASH_COMMAND (exit $?)" >&2' ERR - Run it against staging with
bash -xand aPS4that prints line numbers, fixing everything strict mode trips on (often optional variables needing${VAR:-}). - Restructure so the switch-over is a single atomic step at the end (build in a new release directory, then
ln -sfnand reload), so a failure before that point changes nothing live. - Add a lock (
flock -n) so two people cannot deploy at once.
Why → Most "sometimes" bugs are unchecked failures plus non-atomic changes. Strict mode surfaces them, atomic switching makes them harmless.
Interview tip → Say you would not big-bang rewrite it; you make it observable first, then refactor in small, tested steps.
More on Scenarios: writing and hardening scripts
A script with `#!/bin/sh` works on CentOS but fails on Ubuntu with `Syntax error: "(" unexpected`. What is going on and what do you do?BeginnerYou need a deploy script that humans and CI both call with `--env`, `--version` and `--dry-run`. How do you design its interface?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