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
# !! DRAFT !!
# https://betterdev.blog/minimal-safe-bash-script-template/
# 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
trap cleanup SIGINT SIGTERM ERR EXIT
readonly SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
readonly SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
readonly ERROR_CODE=90
function usage() {
cat <<EOT
Usage: ${script_name} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
cat <<-EOT
Usage: ${SCRIPT_NAME} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
DESCRIPTION
Your description here.
OPTIONS:
@@ -36,53 +41,55 @@ function msg() {
}
function die() {
local -r msg="$1"
local -r code="${2:-90}"
echo "${msg}" >&2
exit "${code}"
local msg="$1"
local code="${2:-$ERROR_CODE}"
echo "$msg" >&2
exit "$code"
}
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
--abc)
abc_option_flag=1
shift
readonly abc_arg="$1"
shift
;;
-a)
a_option_flag=1
shift
readonly a_arg="$1"
shift
;;
-a)
a_option_flag=1
shift
readonly a_arg="$1"
shift
;;
--help | -h)
usage
exit 0
shift
;;
--help|-h)
usage
exit 0
shift
;;
--flag | -f)
flag_option_flag=1
shift
;;
--flag|-f)
flag_option_flag=1
shift
;;
--)
shift
break
;;
--)
shift
break
;;
*)
EXTRA_ARGS+=("$opt")
;;
*)
break
;;
esac
done
done
# join EXTRA_ARGS a ARGS
}
# MAIN