I have a bash script that parses an xml file (name: myFile.xml) that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<params>
<username>jDoe</username>
<password>123abc</password>
<fullname>John Doe</fullname>
<email>johndoe@example.com</email>
<phone>1234567890</phone>
<country>Italy</country>
</params>
In that bash script, I parse each value of the xml file into a variable for further use. So far the bash script looks like that:
for i in $(xmlstarlet select --template --match "//params/*" --value-of "concat(name(),'=\"',text(),'\"')" -n myFile.xml)
do
eval $i
done
#debugging:
echo $username
echo $fullname
echo $password
When I run this script
./myScript.sh
I get the following output:
./myScript.sh: eval: line 34: unexpected EOF while looking for matching `"'
./myScript.sh: eval: line 35: syntax error: unexpected end of file
./myScript.sh: eval: line 34: unexpected EOF while looking for matching `"'
./myScript.sh: eval: line 35: syntax error: unexpected end of file
jDoe
123abc
Apparently, because the tag <fullname>
has a value of 2 words separated by space the script chokes. If I replace it's value "John Doe" with another (with no space) like: "JohnDoe" or "John_Doe" the script works fine!
Any suggestions as to how I can pass to a bash variable a value that contains space?
Of course, I would like to maintain the loop because 1. the actual script has too many parameters and 2. the parameters are not always the same (they vary from one xml file to another)...
(and to cut the long story short, this is what I would like to achieve: fullname="John Doe")