我的XML文件(in.xml)就像:
<Users>
<Host>
<hostAddress>180.144.226.47</hostAddress>
<userName>pwdfe</userName>
<password>hjitre</password>
<instanceCount>2</instanceCount>
</Host>
<Host>
<hostAddress>180.144.226.87</hostAddress>
<userName>trrrer</userName>
<password>jhjjhhj</password>
<instanceCount>3</instanceCount>
</Host>
</Users>
我的shell脚本是:
#!/bin/ksh
for tag in hostAddress userName password instanceCount
do
OUT=`grep $tag in.xml | tr -d '\t' | sed 's/^<.*>\([^<].*\)<.*>$/\1/' `
# This is what I call the eval_trick, difficult to explain in words.
eval ${tag}=`echo -ne \""${OUT}"\"`
done
# So let's stuff the obtained results into 4 different Arrays
H_ARRAY=( `echo ${hostAddress}` )
U_ARRAY=( `echo ${userName}` )
P_ARRAY=( `echo ${password}` )
I_ARRAY=( `echo ${instanceCount}` )
# Ok, time to announce success, let's printout each of the arrays
echo ${H_ARRAY[@]}
echo ${U_ARRAY[@]}
echo ${P_ARRAY[@]}
echo ${I_ARRAY[@]}
执行脚本时,我收到此错误: eval [1]: - ne:找不到[没有这样的文件或目录]
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
一些一般问题:
$(command)
rather than `command`
. eval
(which is evil),只需创建一个函数来提取任意元素(基本上只是你的grep
行)并运行四次。array_variable+=(array_element)
答案 1 :(得分:1)
如果您确实需要echo -e
的效果,并且正在运行ksh93或mksh,则可以使用nameref执行间接分配,从而无需eval
:
typeset -n ref="$tag"
ref=$(echo -ne "$OUT")
如果您使用的是bash,我会建议以下内容(也执行echo -e
的反斜杠序列扩展):
printf -v "$tag" %b "$OUT"