bash 读取和存储 xml 值的脚本

bash script to read & store xml value

我有一个 xml 文件,我想检索 filePath 值并将其存储为变量以用于启动应用程序。这是 xml 文件:

<?xml version="1.0"?>
<resultset statement="SELECT * FROM dms.gamThr_exp
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
    <field name="id">1</field>
    <field name="filePath">/home/drs/Videos/Game.of.Thrones/Game.of.Thrones.S01E01.mkv</field>
 </row>
</resultset>

我已经将这个脚本拼凑在一起,但它不起作用:

#!/bin/bash
myvar=$(echo 'cat //row/field[@name="filePath"]/@value' | xmllint --shell /home/drs/dms/gamThr.xml | awk -F'[="]' '{print $(NF-1)}') 
vlc --fullscreen "$myvar"

任何帮助让它工作的人都将不胜感激!

到select上下文节点的文本节点,使用text()节点测试:

//row/field[@name="filePath"]/text()

使用xmllint--xpath选项传递xpath:

xmllint --xpath '//row/field[@name="filePath"]/text()' /home/drs/dms/gamThr.xml

最后你的脚本应该是这样的:

#!/bin/bash

myvar=$(xmllint --xpath '//row/field[@name="filePath"]/text()' /home/drs/dms/gamThr.xml) 
vlc --fullscreen "$myvar"