Created
May 27, 2017 18:59
-
-
Save Vdragon/03e32e247d99e706452a6927dcfd301f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#shellcheck disable=SC2034 | |
# Comments prefixed by BASHDOC: are hints to specific GNU Bash Manual's section: | |
# https://www.gnu.org/software/bash/manual/ | |
## Makes debuggers' life easier - Unofficial Bash Strict Mode | |
## http://redsymbol.net/articles/unofficial-bash-strict-mode/ | |
## BASHDOC: Shell Builtin Commands - Modifying Shell Behavior - The Set Builtin | |
### Exit prematurely if a command's return value is not 0(with some exceptions), triggers ERR trap if available. | |
set -o errexit | |
### Trap on `ERR' is inherited by shell functions, command substitutions, and subshell environment as well | |
set -o errtrace | |
### Exit prematurely if an unset variable is expanded, causing parameter expansion failure. | |
set -o nounset | |
### Let the return value of a pipeline be the value of the last (rightmost) command to exit with a non-zero status | |
set -o pipefail | |
## Non-overridable Primitive Variables | |
## | |
## BashFAQ/How do I determine the location of my script? I want to read some config files from the same place. - Greg's Wiki | |
## http://mywiki.wooledge.org/BashFAQ/028 | |
RUNTIME_EXECUTABLE_FILENAME="$(basename "${BASH_SOURCE[0]}")" | |
declare -r RUNTIME_EXECUTABLE_FILENAME | |
declare -r RUNTIME_EXECUTABLE_NAME="${RUNTIME_EXECUTABLE_FILENAME%.*}" | |
RUNTIME_EXECUTABLE_DIRECTORY="$(dirname "$(realpath --strip "${0}")")" | |
declare -r RUNTIME_EXECUTABLE_DIRECTORY | |
declare -r RUNTIME_EXECUTABLE_PATH_ABSOLUTE="${RUNTIME_EXECUTABLE_DIRECTORY}/${RUNTIME_EXECUTABLE_FILENAME}" | |
declare -r RUNTIME_EXECUTABLE_PATH_RELATIVE="${0}" | |
declare -r RUNTIME_COMMAND_BASE="${RUNTIME_COMMAND_BASE:-${0}}" | |
### These are the dependencies that are used later and also checked later | |
declare -ar global_indexed_array=(1 2 3) | |
declare -Ar global_associative_array=(\ | |
["apple"]="1" \ | |
["banana"]="2" \ | |
) | |
trap_errexit(){ | |
printf "An error occurred and the script is prematurely aborted\n" 1>&2 | |
return 0 | |
}; declare -fr trap_errexit; trap trap_errexit ERR | |
trap_exit(){ | |
return 0 | |
}; declare -fr trap_exit; trap trap_exit EXIT | |
is_set(){ | |
declare -n array_nameref="${1}" | |
if [ ! -v array_nameref ]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
## init function: program entrypoint | |
init(){ | |
for parameter in global_associative_array global_indexed_array; do | |
# Case: check in function | |
if is_set "${parameter}"; then | |
printf "%s is set\n" "${parameter}" | |
else | |
printf "%s is NOT set\n" "${parameter}" | |
fi | |
# Case: check inline | |
declare -n array_nameref="${parameter}" | |
if [ ! -v array_nameref ]; then | |
printf "%s is NOT set\n" "${parameter}" | |
else | |
printf "%s is set\n" "${parameter}" | |
fi | |
unset -n array_nameref | |
done | |
# exit 0 | |
}; declare -fr init | |
init "${@}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment