Interview Prep/Linux/Scheduling, automation and scripting

Write a safe bash script header for a production maintenance script. What goes in it and why?

IntermediateScenarioScheduling, automation and scripting
Answer

Situation read → Fail fast, clean up, log.

What I'd do →

#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
trap 'echo "ERROR line $LINENO: $BASH_COMMAND" >&2' ERR
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
log() { logger -t maint "$*"; echo "$(date -Is) $*"; }

Why → -e exits on error, -u catches unset variables, pipefail catches failures inside pipes, the ERR trap tells you where it died, and the EXIT trap always cleans up temp files.

Interview tip → Mention shellcheck in CI for every script.

All Linux questions Roadmap: Linux

More on Scheduling, automation and scripting