Chapter 5: Bash Scripting for Automation
5.9 Functions — keep scripts readable
When the file grows past one screen, wrap repeated bits in functions.
log() {
echo "$(date -Iseconds) $*" | tee -a "$LOG_DIR/health.log"
}
health_ok() {
curl -fsS "$APP_URL" >/dev/null
}
Call log "OK" or if health_ok; then …. Name functions after the action. Keep side effects obvious.
Interview line: "I keep ops scripts short, with dry-run, clear exit codes and no secrets in the file."