Developing Complex POSIX Shell Scripts

Complex POSIX shell scripts are production system utilities or reusable tools distinguished from simple scripts by three structural additions: a Logging Subsystem, a Usage/Help function, and Argument Parsing via getopts. Apply them only when the script genuinely needs them.

See common.md for baseline requirements (shebang, safety modes, tooling, POSIX compliance, logic, quoting).

Composition Guide

Pick and compose from reference-code-blocks.md. SHOULD include only what the script actually uses.

  1. Include: Script Identity + Shebang/Safety Modes + main.
  2. Include the Logging Subsystem when output needs severity levels or colour; omit the setter functions if -l / -f flags are not exposed.
  3. Include the Dependency Check when relying on non-standard external commands; it calls log_error, so include the Logging Subsystem as well.
  4. Include the Cleanup Handler when the script allocates resources (temp files, locks, etc.).
  5. Include Usage + Argument Parsing when the script accepts any named flags.