Every script answers a help flag and does nothing else

One of my scripts took its help flag as a hostname and disabled Apache. The one line that now sits at the top of all 45, in bash and in Python.

A terminal. An example script asked for help prints the comment block from the top of its own file and stops. Underneath, the one line of bash that does it. A terminal. An example script asked for help prints the comment block from the top of its own file and stops. Underneath, the one line of bash that does it.

I have a script that brings one of my Linux boxes into line with the other. It takes the source machine as its first argument, pulls that machine’s package list, config files and enabled services over ssh, and makes the box it’s running on match. It needs root for most of that. It had no --help.

Then it got run with --help, by a coding agent that was checking my build guide against the real machines.

What it did

The script took --help as the name of the source host and started work.

Every rsync failed on the bad host name, so no file was overwritten. The ssh calls that ask the source what it has enabled failed too, and they failed quietly: each one came back as an empty list. The Apache step compares what’s enabled locally against that list and disables the difference. Against an empty list the difference is everything, so it disabled all 26 modules, 8 confs and 12 sites, and then stopped with “No MPM loaded”. The same run disabled a systemd timer, ran locale-gen and apt-get update, and deleted Chrome’s lock files while Chrome was open.

Apache carried on serving from memory, so nothing was down. I had a snapshot of the machine’s state from 46 minutes earlier, which included the three lists, and putting it back was a2enmod, a2enconf and a2ensite over those and apache2ctl -t: Syntax OK. A fresh snapshot matched the earlier one.

The line

Every bash script of mine now has this directly under its header comment, before set -u and before anything else:

case ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; esac

It prints the script’s own comment block, from line 2 to the first line that isn’t a comment, with the # stripped, and exits 0. The help text is the header, so there’s only one of them to keep right. With a header like this:

#!/usr/bin/env bash
# rotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.
#
#   rotate-logs            do it
#   rotate-logs --dry-run  list what would be compressed and deleted
case ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; esac
set -eu

you get:

$ rotate-logs --help
rotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.

  rotate-logs            do it
  rotate-logs --dry-run  list what would be compressed and deleted

The Python ones print the module docstring. Where a script doesn’t use argparse, this is the first thing in main(), before any argument is looked at:

if sys.argv[1:2] in (["-h"], ["--help"]):
    print(__doc__)
    raise SystemExit(0)

The repo has 45 executable scripts, 31 in bash and 14 in Python. The Python ones already answered --help. The line went into 25 other bash scripts in the same commit that fixed the first one.

The script itself

The one that caused it got more than a help option. It now refuses, before it does anything:

  • an option it doesn’t know
  • a part name it doesn’t know (it runs in named parts: packages, snaps, services and so on)
  • a source that doesn’t answer over ssh

And the Apache step does nothing when the source’s list comes back empty. That was the real bug. A help option would have stopped this run, but a typo in a real host name would have done exactly the same damage.

The other half

A help option only protects a script that has one. So there’s a second rule, written into the rules my coding agents work under: never run a script to find out whether it takes --help. Read its header first, with sed -n 1,40p.

Featured projects

All projects

Latest writing

All writing