I would like the output of the XMLLINT to get put into an BASH array. But all I can get is a single string. The results will return many hits, none with any pattern that can help parse the returned string.
- I have tried --format and redirect ">" to a text file.
- I have tried xpath all instances
//
and just one/
mcv.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<instance>
<absolutePath>/abc/def</absolutePath>
</instance>
<instance>
<absolutePath>/abc/hij</absolutePath>
</instance>
</root>
mcv.sh
#!/usr/bin/bash
declare -a wwArray=()
wwCount=$(xmllint --xpath 'count(//absolutePath)' "mcv.xml")
printf "wwCount: '%s' \n" ${wwCount}
i=1
while [ $i -le ${wwCount} ];
do
wwExtracted=$(xmllint --xpath '//absolutePath['${i}']/text ()' "mcv.xml")
printf " - absolutePath: '%s' \n" ${wwExtracted}
printf " - index: '%d' \n" ${i}
let i=i+1
done
Running this, the output is:
wwCount: '2'
- absolutePath: '/abc/def/abc/hij'
- index: '1'
XPath set is empty
- absolutePath: ''
- index: '2'
...whereas I would expect it to instead be:
wwCount: '2'
- absolutePath: '/abc/def'
- index: '1'
- absolutePath: '/abc/hij'
- index: '2'
ww=$(...)
is a string assignment, not an array assignment, and thatfor i in $ww
is word-splitting and glob-expanding a string, not iterating over array elements. (Even ifww
is an array, that operation ignores all but the first element, and operates on that first element as a string). – Chitaww
truly were an array, iterating over it would befor i in "${ww[@]}"
, but to make that true you'd need to assign to it in a completely different way. – Chitafor
to run aprintf
string once per array element --printf '%s\n' "${ww[@]}"
would do the trick (if, again, you had actually assigned to an array in the first place). – Chita