Last active
June 2, 2022 21:33
-
-
Save n1kk/dfcd45f91ffe283f78434c9122078ce6 to your computer and use it in GitHub Desktop.
Bash helper tools to find a defined and/or non empty variable from a list of variable names
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
val1="" | |
val2="asd" | |
function firstDefinedName() { | |
for varName in "$@"; do | |
if [[ ! -z "${!varName+____}" ]]; then | |
echo "$varName" | |
break | |
fi | |
done | |
} | |
echo "\""$(firstDefinedName val0 val1 val2)"\"" "== val1" | |
function firstDefinedValue() { | |
for varName in "$@"; do | |
if [[ ! -z "${!varName+____}" ]]; then | |
echo "${!varName}" | |
break | |
fi | |
done | |
} | |
echo "\""$(firstDefinedValue val0 val1 val2)"\"" "== empty string" | |
function firstNonEmptyName() { | |
for varName in "$@"; do | |
if [[ -n "${!varName}" ]]; then | |
echo "$varName" | |
break | |
fi | |
done | |
} | |
echo "\""$(firstNonEmptyName val0 val1 val2)"\"" "== val2" | |
function firstNonEmptyValue() { | |
for varName in "$@"; do | |
if [[ -n "${!varName}" ]]; then | |
echo "${!varName}" | |
break | |
fi | |
done | |
} | |
echo "\""$(firstNonEmptyValue val0 val1 val2)"\"" "== asd" | |
function one() { | |
local val1="qwe" | |
echo "inside function local space with 'local val1=\"qwe\"'" | |
echo "\""$(firstNonEmptyValue val0 val1 val2)"\"" "== qwe" | |
} | |
one |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment