没有--xpath选项的xmllint字符串

问题描述 投票:1回答:1

我有一个看起来像这样的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <role>probe</role>
    <ipconfig>yes</ipconfig>
    <hostname>test</hostname>
    <ip>192.168.2.123</ip>
    <gateway>192.168.2.1</gateway>
    <subnet>255.255.254.0</subnet>
</customer>

我在CentOS7下有脚本来获取值:

role="$(xmllint --xpath "string(//role)" "${xml_path}")"

这非常有效。

现在我必须在CentOS 6环境下使用相同的功能。没有--xpath支持,我无法安装它。

实现同样目标的最简单最简单的方法是什么?

bash centos6 xmllint
1个回答
0
投票

这可能有效:

role=$(echo "cat /customer/role/text()" | xmllint --shell file.xml | grep -v '^/')

输出到变量role

probe
© www.soinside.com 2019 - 2024. All rights reserved.