Bash Safe Script Template
A robust bash script template with error handling.
scriptingtemplateerror-handling
bash
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
log() { echo -e "${GREEN}[INFO]${NC} $1"; }
err() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
cleanup() {
log "Cleaning up..."
# Remove temp files, etc.
}
trap cleanup EXIT
main() {
local input="${1:?Usage: $0 <input>}"
log "Processing: $input"
if [[ ! -f "$input" ]]; then
err "File not found: $input"
exit 1
fi
# Your logic here
log "Done!"
}
main "$@"