set -e killed my scripts when nothing was found
set -euo pipefail at the top of every script is good advice. it also means any command that exits non-zero for a perfectly normal reason takes the whole script down. it got me twice in the lab, and nearly a third time.
grep exits 1 when it finds nothing
my deploy script diffed cron jobs between the repo and the server:
LOCAL_JOBS=$(cron_jobs < "$CRON_ROOT")
cron_jobs strips comments and blank lines (grep -vE '^[[:space:]]*#' | sed ...). one server’s crontab was all comments, so grep matched nothing, exited 1, and with pipefail the whole $( ) failed and set -e killed the deploy halfway. same thing on a server with no crontab at all, because crontab -l exits non-zero when there’s no table.
easy to see:
$ printf '# just comments\n' > ct.txt
$ bash -c 'set -euo pipefail; jobs=$(grep -v "^#" ct.txt); echo reached'
$ echo $?
1
reached never prints.
the backup that wrote nothing
the backup script on one of my servers copied a website folder I’d since retired. cp on a missing path exits 1, strict mode stopped the script before it compressed anything, and the weekly backup produced nothing. nothing told me.
another server’s backup script had the same trap waiting: a sqlite3 line pointing at a database path that had been deleted. it never ran against it, the box was off, but under strict mode it would have aborted the same way.
the fix, and what it costs
for “no match is fine”, || true:
LOCAL_JOBS=$(cron_jobs < "$CRON_ROOT" || true)
for a path that might be gone, check it first: [[ -d ~/docker/website ]] && cp -r ....
|| true has a catch with pipefail on. it covers the whole pipeline, not just grep. the remote side of the diff was:
REMOTE_JOBS=$(ssh "$REMOTE" "sudo -S crontab -l -u root 2>/dev/null" <<< "${N_SERVER_SUDO:-}" | cron_jobs || true)
if ssh fails, that’s swallowed too, REMOTE_JOBS is empty, and the diff says the server has no jobs:
$ bash -c 'set -euo pipefail; R=$(false | grep -v "^#" || true); echo "continued: [$R]"'
continued: []
in a script where an empty list means “remove everything”, that || true is a real bug.
the narrower version only forgives grep’s 1:
cron_jobs() { grep -vE '^[[:space:]]*#' || [[ $? -eq 1 ]]; }
tested: no match passes, a failed command upstream still stops the script.
see also: process tricks, every problem