A safe script skeleton
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
readonly LOG=/var/log/myjob.log
log() { printf '%s %s\n' "$(date '+%F %T')" "$*" | tee -a "$LOG"; }
die() { log "ERROR: $*"; exit 1; }
trap 'die "failed at line $LINENO"' ERR
main() {
[[ $EUID -eq 0 ]] || die "run as root"
log "start"
# work here
log "done"
}
main "$@"
-estop on error,-uerror on unset variables,-o pipefailcatch failures inside pipes- Check scripts with shellcheck — it finds most bugs before they run
Rating
0
0
There are no comments for now.