2017-06-27 86 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"] 
相關問題