将命令的输出保存在数组中
save output of command in array
我用的是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[]" #"ENABLE"
gps= "$chosenOne[]" #"DISABLE"
我遇到类似语法错误的错误:单词意外(预期为“)”)
谁能帮我,我怎样才能将这些参数从 XML 文件保存到数组中?
试试这个:
#!/bin/sh
AMIT=$(grep AMIT -A1 items.xml | awk -F '[<>]' '/description/{print }')
GPS=$(grep GPS -A1 items.xml | awk -F '[<>]' '/description/{print }')
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"]
我用的是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[]" #"ENABLE"
gps= "$chosenOne[]" #"DISABLE"
我遇到类似语法错误的错误:单词意外(预期为“)”) 谁能帮我,我怎样才能将这些参数从 XML 文件保存到数组中?
试试这个:
#!/bin/sh
AMIT=$(grep AMIT -A1 items.xml | awk -F '[<>]' '/description/{print }')
GPS=$(grep GPS -A1 items.xml | awk -F '[<>]' '/description/{print }')
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"]