linux - 如何从xml文件中获取特定数据?

标签 linux bash shell

从命令行,以下命令可以正常工作:

echo 'cat //volume_trustees/*[contains(translate(.,"SMITH","smith"),"smith")]' | xmllint --shell /path/database.xml

但我想将其放入 shell 脚本中,并以搜索项作为参数。 但我不知道如何让它发挥作用:

y=$1

up="$(echo $y | tr 'A-Z' 'a-z')"

lo="$(echo $y | tr 'a-z' 'A-Z')"

echo 'cat //volume_trustees/*[contains(translate(.,$up,$lo),$lo)]' | xmllint --shell /path/database.xml

这会显示以下错误消息:

xmlXPathCompOpEval: parameter error
XPath error : Stack usage errror
XPath error : Invalid operand
XPath error : Stack usage errror
XPath error : Invalid type
xmlXPathEval: 1 object left on the stack
//volume_trustees/*[contains(translate(.,$up,$lo),$lo)]: no such node

谁能帮帮我吗?

最佳答案

您需要在 echo 上加双引号(也应该在 y 赋值上使用双引号)

y="$1"

echo "cat //volume_trustees/*[contains(translate(.,$up,$lo),$lo)]" | xmllint --shell /path/database.xml

echo 字符串括在单引号中可防止 $up、$lo 和 $lo 扩展。

关于linux - 如何从xml文件中获取特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27035674/

相关文章:

c - 防止死锁

linux - kexec 到 ELF x86_64 二进制文件后页表的状态是什么?

mysql - 带线程的 Perl 邮件队列 'out of memory'

regex - 通过 linux shell 执行文本

mysql - Unix Shell 脚本 : Reading and working with MySQL Rows line by line

linux - 读取 : read error: 0: Resource temporarily unavailable when calling scripts using case in bash

linux - 使用端口转发 SSH 连接到本地服务器?

json - jq 在输出中打印字符

bash - 允许其他用户控制进程

linux - 使用 $() 将多个命令输出到变量 Bash 脚本中