template script

main
Andrew Coleman 2024-01-17 11:29:34 -05:00
parent 1d6da564f0
commit d9bb16459f
1 changed files with 150 additions and 0 deletions

150
template.sh Executable file
View File

@ -0,0 +1,150 @@
#!/usr/bin/env bash
# Example Bash Shell Script
# from https://betterdev.blog/minimal-safe-bash-script-template/
set -Eeuo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1
# declare globals as readonly
readonly SCRIPT_NAME
SCRIPT_NAME="$(basename "$0")"
trap cleanup SIGINT SIGTERM ERR EXIT
usage() {
cat <<EOF
Usage: $(basename "$0") [-h] [-v] [-f] -p param_value arg1 [arg2...]
Script description here.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-f, --flag Some flag description
-p, --param Some param description
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z ${NO_COLOR-} ]] && [[ ${TERM-} != "dumb" ]]; then
NOCOLOR='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
# shellcheck disable=SC2034
NOCOLOR='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
# you only need one of the following two functions
# simple function to redirect output to stderr (good)
msg() {
echo >&2 -e "${1-}"
}
# more structured logging (better)
log() {
local -r level="$1"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# colorize your log output here, if desired
echo >&2 -e "${timestamp} [${level}] [$SCRIPT_NAME] ${message}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
log "FATAL" "$msg"
exit "$code"
}
assert_not_empty() {
local -r arg_name="$1"
local -r arg_value="$2"
if [[ -z $arg_value ]]; then
log "ERROR" "The value for '$arg_name' cannot be empty"
exit 1
fi
}
assert_is_installed() {
local -r name="$1"
if [[ ! $(command -v "$name") ]]; then
log "ERROR" "The binary '$name' is required by this script but is not installed or in the system's PATH."
exit 1
fi
}
parse_params() {
while :; do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
set -x
;;
--no-color)
NO_COLOR=1
;;
-f | --flag) # example flag
# FLAG=1
;;
-p | --param) # example named parameter
assert_not_empty "--param", "${2-}"
PARAM="${2-}"
shift
;;
-?*)
die "Unknown option: $1"
;;
*)
break
;;
esac
shift
done
# check required params and arguments
assert_is_installed "bash"
return 0
}
# default values of variables set from parse_params
# to avoid having these variables here, you will need to define the work of
# the script as another function that takes arguments and call it with local
# variables declared in parse_params:
#
# run() {
# local -r flag=$1
# }
#
# parse_params() {
# local flag=0
# ...
# run $flag
# }
FLAG=0
PARAM=''
readonly ARGS=("$@")
parse_params "$@"
setup_colors
# script logic here
log "INFO" "${RED}Read parameters:${NOCOLOR}"
log "INFO" "- flag: ${FLAG}"
log "INFO" "- param: ${PARAM}"
log "INFO" "- arguments: ${ARGS[*]-}"