{
    "type": "article",
    "title": "Every script answers a help flag and does nothing else",
    "canonical": "https://www.dealmobile.co.uk/writing/every-script-answers-help-and-does-nothing-else",
    "path": "/writing/every-script-answers-help-and-does-nothing-else",
    "url": "https://www.dealmobile.co.uk/writing/every-script-answers-help-and-does-nothing-else",
    "markdown": "https://www.dealmobile.co.uk/writing/every-script-answers-help-and-does-nothing-else.md",
    "json": "https://www.dealmobile.co.uk/writing/every-script-answers-help-and-does-nothing-else.json",
    "summary": "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.",
    "published": "2026-11-24T21:05:00+00:00",
    "modified": "2026-11-24T21:05:00+00:00",
    "image": {
        "url": "https://www.dealmobile.co.uk/sites/default/files/content/images/posts/every-script-answers-help-and-does-nothing-else/hero.png",
        "alt": "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."
    },
    "fields": {
        "image_dark": null
    },
    "tags": [
        {
            "name": "Linux",
            "url": "https://www.dealmobile.co.uk/writing/tag/linux",
            "json": "https://www.dealmobile.co.uk/writing/tag/linux.json"
        },
        {
            "name": "Agents",
            "url": "https://www.dealmobile.co.uk/writing/tag/agents",
            "json": "https://www.dealmobile.co.uk/writing/tag/agents.json"
        }
    ],
    "body": {
        "html": "<p>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 <code>--help</code>.</p>\n<p>Then it got run with <code>--help</code>, by a coding agent that was checking my build guide against the real machines.</p>\n<h2 id=\"what-it-did\">What it did</h2>\n<p>The script took <code>--help</code> as the name of the source host and started work.</p>\n<p>Every <code>rsync</code> 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 <code>locale-gen</code> and <code>apt-get update</code>, and deleted Chrome’s lock files while Chrome was open.</p>\n<p>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 <code>a2enmod</code>, <code>a2enconf</code> and <code>a2ensite</code> over those and <code>apache2ctl -t</code>: Syntax OK. A fresh snapshot matched the earlier one.</p>\n<h2 id=\"the-line\">The line</h2>\n<p>Every bash script of mine now has this directly under its header comment, before <code>set -u</code> and before anything else:</p>\n<pre><code class=\"language-bash\">case ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' \"$0\" | sed 's/^# \\{0,1\\}//'; exit 0 ;; esac\n</code></pre>\n<p>It prints the script’s own comment block, from line 2 to the first line that isn’t a comment, with the <code># </code> 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:</p>\n<pre><code class=\"language-bash\">#!/usr/bin/env bash\n# rotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.\n#\n#   rotate-logs            do it\n#   rotate-logs --dry-run  list what would be compressed and deleted\ncase ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' \"$0\" | sed 's/^# \\{0,1\\}//'; exit 0 ;; esac\nset -eu\n</code></pre>\n<p>you get:</p>\n<pre><code>$ rotate-logs --help\nrotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.\n\n  rotate-logs            do it\n  rotate-logs --dry-run  list what would be compressed and deleted\n</code></pre>\n<p>The Python ones print the module docstring. Where a script doesn’t use <code>argparse</code>, this is the first thing in <code>main()</code>, before any argument is looked at:</p>\n<pre><code class=\"language-python\">if sys.argv[1:2] in ([\"-h\"], [\"--help\"]):\n    print(__doc__)\n    raise SystemExit(0)\n</code></pre>\n<p>The repo has 45 executable scripts, 31 in bash and 14 in Python. The Python ones already answered <code>--help</code>. The line went into 25 other bash scripts in the same commit that fixed the first one.</p>\n<h2 id=\"the-script-itself\">The script itself</h2>\n<p>The one that caused it got more than a help option. It now refuses, before it does anything:</p>\n<ul>\n<li>an option it doesn’t know</li>\n<li>a part name it doesn’t know (it runs in named parts: packages, snaps, services and so on)</li>\n<li>a source that doesn’t answer over ssh</li>\n</ul>\n<p>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.</p>\n<h2 id=\"the-other-half\">The other half</h2>\n<p>A help option only protects a script that has one. So there’s a second rule, written into <a href=\"/writing/rules-a-coding-agent-cannot-talk-its-way-round\">the rules my coding agents work under</a>: never run a script to find out whether it takes <code>--help</code>. Read its header first, with <code>sed -n 1,40p</code>.</p>"
    },
    "markdown_text": "---\ntitle: 'Every script answers a help flag and does nothing else'\ncanonical: 'https://www.dealmobile.co.uk/writing/every-script-answers-help-and-does-nothing-else'\nurl: 'https://www.dealmobile.co.uk/writing/every-script-answers-help-and-does-nothing-else'\ntype: Post\npublished: '2026-11-24'\nsummary: '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.'\n---\n\n# Every script answers a help flag and does nothing else\n\nPublished 2026-11-24\n\n![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.](https://www.dealmobile.co.uk/sites/default/files/styles/almanac_shot_md/public/content/images/posts/every-script-answers-help-and-does-nothing-else/hero.png.webp?itok=kUBbbpnV)\n\nI 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`.\n\nThen it got run with `--help`, by a coding agent that was checking my build guide against the real machines.\n\n## What it did\n\nThe script took `--help` as the name of the source host and started work.\n\nEvery `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.\n\nApache 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.\n\n## The line\n\nEvery bash script of mine now has this directly under its header comment, before `set -u` and before anything else:\n\n```bash\ncase ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' \"$0\" | sed 's/^# \\{0,1\\}//'; exit 0 ;; esac\n\n```\n\nIt 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:\n\n```bash\n#!/usr/bin/env bash\n# rotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.\n#\n#   rotate-logs            do it\n#   rotate-logs --dry-run  list what would be compressed and deleted\ncase ${1:-} in -h|--help) sed -n '2,/^[^#]/{/^#/p}' \"$0\" | sed 's/^# \\{0,1\\}//'; exit 0 ;; esac\nset -eu\n\n```\n\nyou get:\n\n```\n$ rotate-logs --help\nrotate-logs: compress yesterday's logs in /var/log/myapp and delete those older than 30 days.\n\n  rotate-logs            do it\n  rotate-logs --dry-run  list what would be compressed and deleted\n\n```\n\nThe 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:\n\n```python\nif sys.argv[1:2] in ([\"-h\"], [\"--help\"]):\n    print(__doc__)\n    raise SystemExit(0)\n\n```\n\nThe 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.\n\n## The script itself\n\nThe one that caused it got more than a help option. It now refuses, before it does anything:\n\n- an option it doesn’t know\n- a part name it doesn’t know (it runs in named parts: packages, snaps, services and so on)\n- a source that doesn’t answer over ssh\n\nAnd 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.\n\n## The other half\n\nA help option only protects a script that has one. So there’s a second rule, written into [the rules my coding agents work under](https://www.dealmobile.co.uk/writing/rules-a-coding-agent-cannot-talk-its-way-round.md): never run a script to find out whether it takes `--help`. Read its header first, with `sed -n 1,40p`.\n\n- **Tags:** [Linux](https://www.dealmobile.co.uk/writing/tag/linux.md), [Agents](https://www.dealmobile.co.uk/writing/tag/agents.md)\n\n---\n\nIndex of this site: [llms.txt](https://www.dealmobile.co.uk/llms.txt)\n"
}