I am currently working on bash script below which reads from an xml document and return the values city on holiday.
xmlconfig="/home/scripts/sample.xml"
dt=$(date +'%Y%m%d')
cty=$(get-holiday-country "$dt" "$xmlconfig")
echo "This are the cities:"
echo "$cty"
get-holiday-country()
{
declare -a arr
for q in `echo 'cat /holiday[@date="$1"]/country/@value' | xmllint --shell "$2" | awk -F\" '/=/ { print $2; }'`
do arr+=("$q")
done
}
However, I am getting a null value on the function I created and when running the for loop alone it is working.
for q in `echo 'cat /holiday[@date="$1"]/country/@value' | xmllint --shell "$2" | awk -F\" '/=/ { print $2; }'`; do echo "$q"; done
Can you help me find out what I did wrong?
declarewhen used in function make the variable local to them. Moreover you don't even try to read thearrarray but actyarray that you do not use anywhere else. Move the declaration outside of the function to avoid using a local variable, and make sure to use the same variable everywheredeclare -gwithin the function to make the array global. And as Aaron suggested, your sample seems to indicate that you aren't using the array you've declared. A function can pass data in three ways - with a numeric exit value, with stdout/stderr, or by populating a global variable. You're doing none of those. If you want$ctyto have any content,echoorprintfthat content within the function.cty=$(get-holiday-country "$dt" "$xmlconfig")