I have many functions which return an array.
function myfunction() {
local -i status=0
local -a statusmsg=()
... do ....
statusmsg=($(do something ...))
if [[ ${status} -eq 0 ]]; then
....
return 0
else
for (( statusmsgline=0; statusmsgline<${#statusmsg[@]}; statusmsgline++ ))
do
printf "%s\n" "${statusmsg[${statusmsgline}]}"
done
return 1
fi
}
in the script I use mapfile as suggested here How to return an array in bash without using globals?
mapfile -t returnmsg <<< "$(myfunction "${param1}" "${param2}" "${paramX}" ...)"
if [ $? -eq 1 ]] ; then
... do something
fi
Using mapfile the array is well returned as it was generated but return code is ever and ever 0
(mapfile exit code) and can't retrieve the status code returned by the function.
I tried to use shopt -s lastpipe
and shopt -so pipefail
but without any success.
Is there any way to retrieve the array from function and the exit code at the same time ?
Kind Regards