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

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

@@ -1,17 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# !! DRAFT !!
# https://betterdev.blog/minimal-safe-bash-script-template/ # https://betterdev.blog/minimal-safe-bash-script-template/
# https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040 # https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040
# https://github.com/koalaman/shellcheck
set -E -o errexit -o nounset -o pipefail set -E -o errexit -o nounset -o pipefail
trap cleanup SIGINT SIGTERM ERR EXIT trap cleanup SIGINT SIGTERM ERR EXIT
readonly SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}") readonly SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
readonly SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) readonly SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
readonly ERROR_CODE=90
function usage() { function usage() {
cat <<EOT cat <<-EOT
Usage: ${script_name} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag] Usage: ${SCRIPT_NAME} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
DESCRIPTION DESCRIPTION
Your description here. Your description here.
OPTIONS: OPTIONS:
@@ -36,53 +41,55 @@ function msg() {
} }
function die() { function die() {
local -r msg="$1" local msg="$1"
local -r code="${2:-90}" local code="${2:-$ERROR_CODE}"
echo "${msg}" >&2 echo "$msg" >&2
exit "${code}" exit "$code"
} }
function parse_params() { function parse_params() {
local -r args=("${@}") local EXTRA_ARGS=()
while true; do for ARG in "$@"; do
case "$ARG" in
--abc)
abc_option_flag=1
shift
readonly abc_arg="$1"
shift
;;
case "$1" in -a)
--abc) a_option_flag=1
abc_option_flag=1 shift
shift readonly a_arg="$1"
readonly abc_arg="$1" shift
shift ;;
;;
-a) --help | -h)
a_option_flag=1 usage
shift exit 0
readonly a_arg="$1" shift
shift ;;
;;
--help|-h) --flag | -f)
usage flag_option_flag=1
exit 0 shift
shift ;;
;;
--flag|-f) --)
flag_option_flag=1 shift
shift break
;; ;;
--) *)
shift EXTRA_ARGS+=("$opt")
break ;;
;;
*)
break
;;
esac esac
done done
# join EXTRA_ARGS a ARGS
} }
# MAIN # MAIN