2017-06-27 83 views
0

我正在使用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" 

我有错误,如语法错误:意外字(预期“)”) 任何人可以帮助我,我怎么可以保存从XML文件中的这些参数数组中?

+0

检查细节/ 137566/arrays-in-unix-bourne-shell – Fidel

+0

你也可以'pkg install bash'。 – arrowd

+0

谢谢菲德尔为您解答。它真的帮助了我 – Hanka

回答

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} 

如果您有蟒蛇,这也可能工作:在https://unix.stackexchange.com/questions提供

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"] 
相关问题