1

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?

5
  • declare when used in function make the variable local to them. Moreover you don't even try to read the arr array but a cty array 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 everywhere Commented Dec 12, 2019 at 9:44
  • 1
    In bash 4+ you can declare -g within 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 $cty to have any content, echo or printf that content within the function. Commented Dec 12, 2019 at 9:55
  • You script calls the function before it was declared.: cty=$(get-holiday-country "$dt" "$xmlconfig") Commented Dec 12, 2019 at 14:14
  • Please share sample input file. Not easy to replicate problem without data. Commented Dec 12, 2019 at 14:14
  • I have solved my problem. I will post it in the answer. Thank you for your help guys. Commented Dec 13, 2019 at 7:41

1 Answer 1

1

Sample XML file:

<holiday date="20191225">
    <country value="US">
        <day value="today">
            <job name="USJOB1"></job>
            <job name="USJOB2"></job>
            <job name="USJOB3"></job>
        </day>
    </country>
    <country value="PH">
        <day value="today">
            <job name="PHJOB1"></job>
            <job name="PHJOB2"></job>
            <job name="PHMJOB3"></job>
        </day>
    </country>
</holiday>

I have made changes from the comments I received and below is the final code.

get-holiday-country()
{
        arr=()
        for q in `echo "cat /holiday[@date="$1"]/country/@value" | xmllint --shell "$2" | awk -F\" '/=/ { print $2; }'`
        do
                arr+=("$q")
        done
        echo ${arr[*]}
}

xmlconfig="/home/scripts/sample.xml"
dt=$(date +'%Y%m%d')

cty=$(get-holiday-country "$dt" "$xmlconfig")
echo "These are the cities:"
echo "$cty"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.