V8.13.048.2025.10.06
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
This commit is contained in:
@@ -13,25 +13,38 @@
|
||||
guard_sourcing
|
||||
|
||||
#######################################
|
||||
# Unbound Variable Check and call Trap on ERR
|
||||
# Unbound variable check and call trap on 'ERR'.
|
||||
# Globals:
|
||||
# ERR_UNBOUNDVAR
|
||||
# Arguments:
|
||||
# $1: VAR_NAME to check
|
||||
# Returns:
|
||||
# "${ERR_UNBOUNDVAR}"
|
||||
# {ERR_UNBOUNDVAR: on failure
|
||||
#######################################
|
||||
check_var() {
|
||||
declare var_name_to_check="$1"
|
||||
|
||||
if [[ -n "${!var_name_to_check+exists}" ]]; then
|
||||
|
||||
if [[ -n "${!var_name_to_check}" ]]; then
|
||||
|
||||
printf "\e[92m✅ Variable: '%s' exists and is NOT empty: '%s' \e[0m\n" "${var_name_to_check}" "${!var_name_to_check}"
|
||||
|
||||
else
|
||||
|
||||
printf "\e[92m✅ Variable: '%s' exists but is empty. \e[0m\n" "${var_name_to_check}"
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
|
||||
printf "\e[91m❌ Variable: '%s' is not declared. Exiting Script. \e[0m\n" "${var_name_to_check}" >&2
|
||||
|
||||
return "${ERR_UNBOUNDVAR}"
|
||||
|
||||
fi
|
||||
}
|
||||
### Prevents accidental 'unset -f'.
|
||||
# shellcheck disable=SC2034
|
||||
readonly -f check_var
|
||||
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
|
||||
|
||||
Reference in New Issue
Block a user