Two bash traps that only bite on a fresh box

Title card: Two bash traps that only bite on a fresh box

Both of these have the same property. The script is fine. It’s been fine for weeks on every machine I own. Then it meets a clean server on its first run and dies in the middle without a word.

I write a lot of install scripts that run once on a brand new Linux box, and they all start with set -euo pipefail. That’s the right call. It also means the script is exactly as strict as the least-tested corner of it, and the least-tested corner is always whatever only happens when something is empty.

Trap one: the counter that exits the script

set -e

count=0
for f in /etc/example/*.conf; do
    [ -e "$f" ] || continue
    ((count++))
done
echo "found $count"

On a box with files in that directory, you’d never notice. On the first pass through the loop, count is 0, and ((count++)) is a post-increment: it evaluates to the old value, 0. In an arithmetic command, a result of 0 counts as false and the command returns exit status 1. Under set -e, a command returning 1 kills the script.

Which means the counter that works from the second iteration onward kills the script on the first. The script just stops, with no message, after the first pass. You can reproduce it in one line:

bash -c 'set -e; n=0; ((n++)); echo survived'; echo "rc=$?"

That prints rc=1 and no “survived”. Start at 1 instead and it’s fine, which is exactly what makes it so slippery.

The fix I use now is boring enough that I stopped thinking about it:

count=$((count + 1))

Assignment from an arithmetic expansion always succeeds, whatever the value. (: $((count++)) also works, but it reads like line noise to whoever opens the script next.)

Trap two: crontab -l on a user with no crontab

The install script adds a cron entry for the service user. The usual idiom:

( crontab -u "$SYS_USER" -l 2>/dev/null; echo "$CRON_LINE" ) | crontab -u "$SYS_USER" -

Read the current crontab, append a line, feed it all back in. On a box where that user already has a crontab, no problem. On a fresh one, crontab -l has nothing to print and exits 1.

The subshell inherits the script’s strict mode. So the exit 1 from crontab -l ends the subshell right there, before the echo ever runs. What goes down the pipe is empty. And pipefail then reports the subshell’s non-zero status as the status of the whole pipeline, which trips the outer set -e, and the installer stops between two log lines:

Installing cron job...

and never prints the next one, “Cron job installed”. No error. Nothing. The box was fresh, so nothing else had happened to compare it with.

Here’s the shape on its own:

bash -c 'set -euo pipefail; ( false; echo "line" ) | cat; echo after'; echo "rc=$?"

The false stands in for crontab -l. line never prints and neither does after.

The fix is to say out loud that the first command is allowed to fail:

( crontab -u "$SYS_USER" -l 2>/dev/null || true; echo "$CRON_LINE" ) | crontab -u "$SYS_USER" -

Now the subshell carries on to the echo whether or not there was an existing crontab.

What they have in common

Strict mode doesn’t make a script safer so much as it moves the bug to the first time a command returns something unusual. A counter at zero. A listing of nothing. A grep that finds no match, which is a perfectly normal answer and exits 1 anyway.

I now test install scripts on a genuinely empty box before I trust them, not a box I’ve been using. Anything of the shape ( thing_that_may_fail; other_thing ) | consumer gets a second look. It’s a small habit. It would have saved me an afternoon.

Leave a Reply

Your email address will not be published. Required fields are marked *