Possible fix for Bash 3.2

This commit is contained in:
Tyler Akins 2024-10-23 18:38:59 -05:00
parent 7e86c1a5f5
commit e0b3b95eba
No known key found for this signature in database
GPG Key ID: 8F3B8C432F4393BD

28
mo
View File

@ -1002,25 +1002,45 @@ mo::isArrayIndexValid() {
# Can not use logic like this in case invalid variable names are passed.
# [[ "${!1-a}" == "${!1-b}" ]]
#
# Using logic like this gives false positives.
# Using logic like this gives false positives. Also, this is not supported on
# Bash 3.2 and the script parsing will error before any commands are executed.
# [[ -v "$a" ]]
#
# Declaring a variable is not the same as assigning the variable.
# export x
# declare -p x # Output: declare -x x
# # Bash 3.2 returns error code 1 and outputs:
# # bash: declare: x: not found
# export y=""
# declare -p y # Output: declare -x y=""
# unset z
# declare -p z # Error code 1 and output: bash: declare: z: not found
#
# Returns true (0) if the variable is set, 1 if the variable is unset.
mo::isVarSet() {
if declare -p "$1" &> /dev/null && [[ -v "$1" ]]; then
unset MO_VAR_TEST_FOR_DECLARE
if declare -p "MO_VAR_TEST_FOR_DECLARE" &> /dev/null; then
mo::debug "Using declare -p and [[ -v ]] for variable checks"
# More recent Bash
mo::isVarSet() {
# Do not convert this to [[, otherwise Bash 3.2 will fail to parse the
# script.
if declare -p "$1" &> /dev/null && test -v "$1"; then
return 0
fi
return 1
}
}
else
mo::debug "Using declare -p for variable checks"
# Bash 3.2
mo::isVarSet() {
if declare -p "$1" &> /dev/null; then
return 0
fi
return 1
}
fi
# Internal: Determine if a value is considered truthy.