我是 bash 中 XML 決議的新手,我想決議下面的 xml 以獲取 ident、host 和 username 屬性的值。
<config>
<connectionstring>
<ftpconfig ident="testftp" host="ftphost" username="456def" password="abc123" localdir="/local/dir/test" />
</connectionstring>
</config>
到目前為止,我可以使用 xmllint 獲取單個屬性的值。但是,這就是我想要實作的。
第一個。使用 ident 屬性獲取主機值。--> 我是用這個實作的。
hostv=$(echo 'cat /config/connectionstring/ftpconfig[@ident="testftp"]/@host' | xmllint --shell myxml.xml | awk -F\" '/=/ { print $2; }')
第二。使用 ident 和 host 屬性獲取用戶名值——>這就是我卡住的地方。我嘗試了不同的方法,如下所示。
userv=$(echo 'cat /config/connectionstring/ftpconfig[@ident="testftp"]/@host=\""$hostv"\"/@username' | xmllint --shell myxml.xml | awk -F\" '/=/ { print $2; }')
在此先感謝您的幫助。
uj5u.com熱心網友回復:
那這個呢?
xmllint --xpath '/config/connectionstring/ftpconfig[@ident="testftp" and @host="ftphost"]/@username' myxml.xml
username="456def"
并且僅獲取username您可以使用的屬性的值string():
xmllint --xpath 'string(/config/connectionstring/ftpconfig[@ident="testftp" and @host="ftphost"]/@username)' file.xml
456def
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461197.html
上一篇:腳本不適用于高性能計算機
