#!/bin/bash
# A template for safe bash scripts with error handling
set -euo pipefail # Exit on error, undefined vars, pipe failures
IFS=$'\n\t' # Safer word splitting
# Cleanup function
cleanup() {
local exit_code=$?
echo "Cleaning up..."
# Add cleanup commands here
exit $exit_code
}
trap cleanup EXIT
# Logging functions
log_info() { echo "[INFO] $(date '+%Y-%m-%d %H:%M:%S') $*"; }
log_error() { echo "[ERROR] $(date '+%Y-%m-%d %H:%M:%S') $*" >&2; }
# Check required commands
require_cmd() {
if ! command -v "$1" &> /dev/null; then
log_error "Required command not found: $1"
exit 1
fi
}
# Main script
main() {
log_info "Starting script..."
require_cmd curl
require_cmd jq
# Your script logic here
log_info "Script completed successfully"
}
main "$@"