Write Bash scripts that fail clearly
Make a small operational script easier to inspect and rerun.
Make assumptions explicit
This example targets Bash on Linux and reports local filesystem capacity. It does not modify services. Prefer small scripts with a documented purpose, required tools, expected privileges, and defined outputs.
Check the prerequisite
Strict mode helps expose failures, but it is not a substitute for understanding command exit codes. In particular, set -e has exceptions in conditionals and pipelines. Check important operations explicitly.
#!/usr/bin/env bash
set -euo pipefail
command -v df >/dev/null || { echo "df is required" >&2; exit 1; }
printf "Filesystem capacity on %s\n" "$(hostname)"
df -hPKeep diagnostics useful
Write diagnostic messages to stderr and preserve a nonzero exit status when an operation fails. Avoid set -x around secrets. Quote variable expansions and use temporary files carefully if the script grows beyond this read-only example.
Test the failure path
Run ShellCheck when available, then test missing prerequisites, permission failures, and invalid inputs in a disposable environment. If a future version changes state, add a preview mode and document what is safe to rerun.