I use following code to verify if variable is not empty and this works fine
VAR=(var1 var2 var3)
chec_var() {
for item in "${@}"; do
if [ -z "${!item}" ]; then
returncode=1
break
else
returncode=0
fi
done
}
all_vars() {
if chec_var "${VAR[@]}"; then
if [[ ${returncode} -gt 0 ]]; then
echo "error"
exit 1
else
echo "OK"
fi
fi
}
all_vars
The problem stars when I add set -Euo pipefail at the end of the script.
Than when one of variables is not present I get following error:
/script line number: !item unbound variable
but it should display "echo error" instead message from pipefail
Another time when all variables are present than it checks only first variable and displays
"echo OK"
instead of going through the loop and check all variables
Question:
What am I doing wrong here?
How to loop through all variables and once all are check to display OK when -Euo pipefail is enabled?
Modified script with pipefail looks as following:
VAR=(var1 var2 var3)
chec_var() {
for item in "${@}"; do
if [ -z "${!item}" ]; then
return 1
break
else
return 0
fi
done
}
all_vars() {
if chec_var "${VAR[@]}"; then
if [[ ${returncode} -gt 0 ]]; then
echo "error"
exit 1
else
echo "OK"
fi
fi
}
set -Euo pipefail
all_vars
set
line goes at the end of the script? – choroba Aug 12 '21 at 11:35-E
makes the ERR trap inherited by functions. I don't see anytrap
defined in your script, though. – choroba Aug 12 '21 at 11:41