我有一个带有用户数据的xml文件(文件名:myFile.xml)。
<?xml version="1.0" encoding="utf-8"?>
<params>
<username>jDoe</username>
<password>abc123</password>
<firstname>John</firstname>
<lastname>Doe</lastname>
<email>jdoe@example.com</email>
<country>Germany</country>
</params>
我可以在bash脚本中打开它,并使用“ for”循环来迭代其内容:
for i in $(xmlstarlet select -t -v '/params/*' myFile.xml)
do
echo $i
done
当我运行它时,我得到:
jDoe
abc123
John
Doe
jdoe@example.com
Germany
如何将每个值与其相对名称相关联,并创建一个bash脚本变量,如下所示:
username="jDoe"
password="abc123"
firstname="John"
lastname="Doe"
email="jdoe@example.com"
country="Germany"
换句话说,我想为每个标签读取它的名称和值,然后在其中创建一个bash变量。即:
tagname="value"
我更喜欢遍历这些标签,因为它们比本示例要多得多,并且并不总是相同。
有什么建议吗?
答案 0 :(得分:5)
xmlstarlet select --template --match "//params/*" --value-of "concat(name(),'=\"',text(),'\"')" -n file.xml
输出:
username="jDoe" password="abc123" firstname="John" lastname="Doe" email="jdoe@example.com" country="Germany"