保存数组中的命令输出

时间:2017-06-27 13:39:05

标签: xml shell freebsd

我正在使用FreeBSD服务器,哪里没有bash,如何在数组中保存命令? 我有命令,它有效grep '<description' amitOrServer.xml | cut -f2 -d">" | cut -f1 -d"<"

我试图从xml文件中保存<description />中的变量。 XML文件如下所示:

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

我需要在变量中保存DISABLE参数,以便稍后在shell脚本中使用它们。

我在变量中保存参数的脚本。

 #!/bin/sh

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

我遇到类似语法错误的错误:word unexpected(expecting“)”) 任何人都可以帮助我,如何从数组中的XML文件中保存这些参数?

1 个答案:

答案 0 :(得分:0)

尝试一下:

#!/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}

如果您有python,这可能也有效:

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"]