0

I'm using FreeBSD server, where is no bash, how can I save command in an array? Ive got command, which works grep '<description' amitOrServer.xml | cut -f2 -d">" | cut -f1 -d"<"

Im trying to save variables in <description /> from xml file. XML file looks like this:

<amitOrServer>
 <item> 
  <title>AMIT</title>
  <description>DISABLE</description> 
 </item> 
 <item> 
  <title>GPS</title> 
  <description>DISABLE</description>  
 </item>  
</amitOrServer>

I need to save DISABLE parameters in the variable to work with them later in the shell script.

a script where I save parameters in variables.

 #!/bin/sh

    chosenOne=( $(grep '<description' amitOrServer.xml | cut -f2 -d">" | cut -f1 -d"<") )
    amit= "$chosenOne[$1]" #"ENABLE"
    gps= "$chosenOne[$2]" #"DISABLE"

I've got error like Syntax error: word unexpected (expecting ")") Can anybody help me, how can I save these parameters from XML file in the array?

Haniku
  • 671
  • 1
  • 7
  • 16

1 Answers1

0

Give a try to this:

#!/bin/sh

AMIT=$(grep AMIT -A1 items.xml | awk -F '[<>]' '/description/{print $3}')
GPS=$(grep GPS -A1 items.xml | awk -F '[<>]' '/description/{print $3}')

echo ${AMIT}
echo ${GPS}

In case you have python, this may also work:

from xml.dom import minidom

xmldoc = minidom.parse('items.xml')
itemlist = xmldoc.getElementsByTagName('item')

out = {}
for i in itemlist:
    title = i.getElementsByTagName('title')[0].firstChild.nodeValue
    description = i.getElementsByTagName('description')[0].firstChild.nodeValue
    out[title] = description

print out
print out["AMIT"]
print out["GPS"]
nbari
  • 25,603
  • 10
  • 76
  • 131