V8.00.000.2025.06.17
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
This commit is contained in:
@@ -13,24 +13,36 @@
|
||||
guard_sourcing
|
||||
|
||||
#######################################
|
||||
# Unbound Variable Check and call Trap on ERR.
|
||||
# Unbound Variable Check.
|
||||
# Globals:
|
||||
# ERR_UNBOUND_VARIABLE
|
||||
# GRE
|
||||
# NL
|
||||
# RED
|
||||
# RES
|
||||
# Arguments:
|
||||
# 1: VAR_NAME to check
|
||||
# 1: VAR_NAME to check NOT the variable itself.
|
||||
# Returns:
|
||||
# ERR_UNBOUND_VARIABLE
|
||||
#######################################
|
||||
check_var() {
|
||||
declare var_name_to_check="$1"
|
||||
if [[ -n "${!var_name_to_check+exists}" ]]; then
|
||||
|
||||
### First check: Is the passed-in variable name itself set?
|
||||
if ! [[ -v var_name_to_check ]]; then
|
||||
printf "%b❌ Error: Variable NOT set: '%s'. Exiting Script. %b%b" "${RED}" "${var_name_to_check}" "${RES}" "${NL}" >&2
|
||||
return "${ERR_UNBOUND_VARIABLE}"
|
||||
fi
|
||||
|
||||
### Second check: Is the variable referenced by var_name_to_check declared?
|
||||
if [[ -v "${var_name_to_check}" ]]; 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}"
|
||||
printf "%b✅ Variable: '%s' exists and is NOT empty: '%s'. %b%b" "${GRE}" "${var_name_to_check}" "${!var_name_to_check}" "${RES}" "${NL}"
|
||||
else
|
||||
printf "\e[92m✅ Variable: '%s' exists but is empty. \e[0m\n" "${var_name_to_check}"
|
||||
printf "%b✅ Variable: '%s' exists but is empty. %b%b" "${GRE}" "${var_name_to_check}" "${RES}" "${NL}"
|
||||
fi
|
||||
else
|
||||
printf "\e[91m❌ Variable: '%s' is not declared. Exiting Script. \e[0m\n" "${var_name_to_check}" >&2
|
||||
printf "%b❌ Error: Variable NOT declared: '%s'. Exiting Script. %b%b" "${RED}" "${var_name_to_check}" "${RES}" "${NL}" >&2
|
||||
return "${ERR_UNBOUND_VARIABLE}"
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user