2013-11-28 25 views
0

這是代碼我試圖讓工作:我在做什麼錯? XML和bash

echo "type in code" 
read code1 
echo (xmllint --xpath '/rates/currency[code="$code1"]/rate/text()' rates.xml) 

它打印「的XPath設置爲空」 它應該打印出來,從我的XML文檔的速度。打印的費率由貨幣代碼(澳元,美元,歐元等)決定。如果輸入的內容如下:

echo (xmllint --xpath '/rates/currency[code="GBP"]/rate/text()' rates.xml) 

這將回顯貨幣代碼是GBP的正確匯率。

這是爲什麼?如何解決它?

回答

1

周圍使用查詢字符串雙引號,這樣$code被擴展到它的價值裏面的字符串:

echo $(xmllint --xpath "/rates/currency[code=\"$code1\"]/rate/text()" rates.xml) 
+0

謝謝,幫了很多。現在遇到另一個簡單的問題(我在腳本中很新)。我如何乘以下面的行,比如說3? $(xmllint --xpath'/ rates/currency ['code =「\」$ code1 \「'']/rate/text()'rates.xml) – user2939526

+0

@ user2939526不客氣。只需爲此提出一個新問題,最有可能得到回答。 – 2013-11-28 16:24:35

1

交易所引號分隔xmllint --xpath "/rates/currency[code='$code1']/rate/text()" rates.xml

+0

謝謝,幫了很多。現在遇到另一個簡單的問題(我在腳本中很新)。我如何乘以下面的行,比如說3? $(xmllint --xpath'/ rates/currency ['code =「\」$ code1 \「'']/rate/text()'rates.xml) – user2939526