Bash template script - change line end character
This commit is contained in:
229
scripts/bash-template.sh
Normal file → Executable file
229
scripts/bash-template.sh
Normal file → Executable file
@@ -1,111 +1,118 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# https://betterdev.blog/minimal-safe-bash-script-template/
|
||||
# https://medium.com/better-programming/my-minimal-safe-bash-script-template-300759114040
|
||||
|
||||
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)
|
||||
|
||||
function usage() {
|
||||
cat <<EOT
|
||||
Usage: ${script_name} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
|
||||
DESCRIPTION
|
||||
Your description here.
|
||||
OPTIONS:
|
||||
-h, --help
|
||||
Print this help and exit.
|
||||
-f, --flag
|
||||
Description for flag option.
|
||||
-a
|
||||
Description for the -a option.
|
||||
--abc
|
||||
Description for the --abc option.
|
||||
EOT
|
||||
}
|
||||
|
||||
function cleanup() {
|
||||
trap - SIGINT SIGTERM ERR EXIT
|
||||
# script cleanup here
|
||||
}
|
||||
|
||||
function msg() {
|
||||
echo >&2 -e "${1-}"
|
||||
}
|
||||
|
||||
function die() {
|
||||
local -r msg="$1"
|
||||
local -r code="${2:-90}"
|
||||
echo "${msg}" >&2
|
||||
exit "${code}"
|
||||
}
|
||||
|
||||
function parse_params() {
|
||||
local -r args=("${@}")
|
||||
|
||||
while true; do
|
||||
|
||||
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
|
||||
;;
|
||||
|
||||
--help|-h)
|
||||
usage
|
||||
exit 0
|
||||
shift
|
||||
;;
|
||||
|
||||
--flag|-f)
|
||||
flag_option_flag=1
|
||||
shift
|
||||
;;
|
||||
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# MAIN
|
||||
|
||||
a_option_flag=0
|
||||
abc_option_flag=0
|
||||
flag_option_flag=0
|
||||
|
||||
|
||||
parse_params "$@"
|
||||
|
||||
if ((flag_option_flag)); then
|
||||
echo "flag option set"
|
||||
fi
|
||||
|
||||
if ((abc_option_flag)); then # Check if the flag options are set or ON:
|
||||
# Logic for when --abc is set.
|
||||
# "${abc_arg}" should also be set.
|
||||
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
|
||||
#!/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]
|
||||
DESCRIPTION
|
||||
Your description here.
|
||||
OPTIONS:
|
||||
-h, --help
|
||||
Print this help and exit.
|
||||
-f, --flag
|
||||
Description for flag option.
|
||||
-a
|
||||
Description for the -a option.
|
||||
--abc
|
||||
Description for the --abc option.
|
||||
EOT
|
||||
}
|
||||
|
||||
function cleanup() {
|
||||
trap - SIGINT SIGTERM ERR EXIT
|
||||
# script cleanup here
|
||||
}
|
||||
|
||||
function msg() {
|
||||
echo >&2 -e "${1-}"
|
||||
}
|
||||
|
||||
function die() {
|
||||
local msg="$1"
|
||||
local code="${2:-$ERROR_CODE}"
|
||||
echo "$msg" >&2
|
||||
exit "$code"
|
||||
}
|
||||
|
||||
function parse_params() {
|
||||
local EXTRA_ARGS=()
|
||||
|
||||
for ARG in "$@"; do
|
||||
case "$ARG" in
|
||||
--abc)
|
||||
abc_option_flag=1
|
||||
shift
|
||||
readonly abc_arg="$1"
|
||||
shift
|
||||
;;
|
||||
|
||||
-a)
|
||||
a_option_flag=1
|
||||
shift
|
||||
readonly a_arg="$1"
|
||||
shift
|
||||
;;
|
||||
|
||||
--help | -h)
|
||||
usage
|
||||
exit 0
|
||||
shift
|
||||
;;
|
||||
|
||||
--flag | -f)
|
||||
flag_option_flag=1
|
||||
shift
|
||||
;;
|
||||
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
|
||||
*)
|
||||
EXTRA_ARGS+=("$opt")
|
||||
;;
|
||||
|
||||
esac
|
||||
done
|
||||
|
||||
# join EXTRA_ARGS a ARGS
|
||||
}
|
||||
|
||||
# MAIN
|
||||
|
||||
a_option_flag=0
|
||||
abc_option_flag=0
|
||||
flag_option_flag=0
|
||||
|
||||
|
||||
parse_params "$@"
|
||||
|
||||
if ((flag_option_flag)); then
|
||||
echo "flag option set"
|
||||
fi
|
||||
|
||||
if ((abc_option_flag)); then # Check if the flag options are set or ON:
|
||||
# Logic for when --abc is set.
|
||||
# "${abc_arg}" should also be set.
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user