🐧 Linux / Shell Scripting
Advanced
How do you write robust, production-grade shell scripts?
Answer
Key practices for production scripts: (1) Strict mode: set -euo pipefail at the top. (2) Cleanup traps: trap 'rm -f "$tmpfile"' EXIT. (3) Use mktemp for temp files: tmpfile=$(mktemp). (4) Quote all variables: "$var", "$@". (5) Check dependencies: command -v jq &>/dev/null || { echo "jq required"; exit 1; }. (6) Atomic file writes: write to a temp file then mv to the target. (7) Locking: use flock to prevent concurrent runs. (8) Logging: timestamp all output. (9) Idempotency: safe to run multiple times. (10) Static analysis: run shellcheck before deploying. (11) Use absolute paths for all commands in cron/systemd contexts.
Previous
What is the /proc filesystem?
Next
What is the difference between bash and POSIX sh scripting?