Bash template script - change line end character

This commit is contained in:
Martin Blazik
2021-01-15 18:59:37 +01:00
parent 53f1908b71
commit 62b68168dd

229
scripts/bash-template.sh Normal file → Executable file
View File

@@ -1,111 +1,118 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# https://betterdev.blog/minimal-safe-bash-script-template/ # !! DRAFT !!
# https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040
# https://betterdev.blog/minimal-safe-bash-script-template/
set -E -o errexit -o nounset -o pipefail # https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040
trap cleanup SIGINT SIGTERM ERR EXIT # https://github.com/koalaman/shellcheck
readonly SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
readonly SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) set -E -o errexit -o nounset -o pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
function usage() {
cat <<EOT readonly SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
Usage: ${script_name} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag] readonly SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
DESCRIPTION readonly ERROR_CODE=90
Your description here.
OPTIONS: function usage() {
-h, --help cat <<-EOT
Print this help and exit. Usage: ${SCRIPT_NAME} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
-f, --flag DESCRIPTION
Description for flag option. Your description here.
-a OPTIONS:
Description for the -a option. -h, --help
--abc Print this help and exit.
Description for the --abc option. -f, --flag
EOT Description for flag option.
} -a
Description for the -a option.
function cleanup() { --abc
trap - SIGINT SIGTERM ERR EXIT Description for the --abc option.
# script cleanup here EOT
} }
function msg() { function cleanup() {
echo >&2 -e "${1-}" trap - SIGINT SIGTERM ERR EXIT
} # script cleanup here
}
function die() {
local -r msg="$1" function msg() {
local -r code="${2:-90}" echo >&2 -e "${1-}"
echo "${msg}" >&2 }
exit "${code}"
} function die() {
local msg="$1"
function parse_params() { local code="${2:-$ERROR_CODE}"
local -r args=("${@}") echo "$msg" >&2
exit "$code"
while true; do }
case "$1" in function parse_params() {
--abc) local EXTRA_ARGS=()
abc_option_flag=1
shift for ARG in "$@"; do
readonly abc_arg="$1" case "$ARG" in
shift --abc)
;; abc_option_flag=1
shift
-a) readonly abc_arg="$1"
a_option_flag=1 shift
shift ;;
readonly a_arg="$1"
shift -a)
;; a_option_flag=1
shift
--help|-h) readonly a_arg="$1"
usage shift
exit 0 ;;
shift
;; --help | -h)
usage
--flag|-f) exit 0
flag_option_flag=1 shift
shift ;;
;;
--flag | -f)
--) flag_option_flag=1
shift shift
break ;;
;;
--)
*) shift
break break
;; ;;
esac
done *)
} EXTRA_ARGS+=("$opt")
;;
# MAIN
esac
a_option_flag=0 done
abc_option_flag=0
flag_option_flag=0 # join EXTRA_ARGS a ARGS
}
parse_params "$@" # MAIN
if ((flag_option_flag)); then a_option_flag=0
echo "flag option set" abc_option_flag=0
fi flag_option_flag=0
if ((abc_option_flag)); then # Check if the flag options are set or ON:
# Logic for when --abc is set. parse_params "$@"
# "${abc_arg}" should also be set.
echo "Using --abc option -> arg: [${abc_arg}]" if ((flag_option_flag)); then
fi echo "flag option set"
fi
if ((a_option_flag)); then
# Logic for when -a is set. if ((abc_option_flag)); then # Check if the flag options are set or ON:
# "${a_arg}" should also be set. # Logic for when --abc is set.
echo "Using -a option -> arg: [${a_arg}]" # "${abc_arg}" should also be set.
fi echo "Using --abc option -> arg: [${abc_arg}]"
fi
if ((a_option_flag)); then
# Logic for when -a is set.
# "${a_arg}" should also be set.
echo "Using -a option -> arg: [${a_arg}]"
fi