![如何在shell腳本中根據屬性檔值取得config.XML欄位值?](https://rvso.com/image/1000396/%E5%A6%82%E4%BD%95%E5%9C%A8shell%E8%85%B3%E6%9C%AC%E4%B8%AD%E6%A0%B9%E6%93%9A%E5%B1%AC%E6%80%A7%E6%AA%94%E5%80%BC%E5%8F%96%E5%BE%97config.XML%E6%AC%84%E4%BD%8D%E5%80%BC%EF%BC%9F%20.png)
我有一個名為 config.xml 的 XML 文件
<builders>
<hudson.tasks.Shell>
<command>$RA_CHEKOUT_SHELL_COMMAND</command>
</hudson.tasks.Shell>
</builders>
這是我的屬性文件內容
建構.prop
這是為了檢查 Jenkins 的工作。這裡我們要進行結帳操作。
外殼腳本
在這裡,我逐行讀取屬性文件,並將屬性檔案值指派給變量,並使用 config.xml 檔案欄位中的值。
file="/var/lib/jenkins/workspace/Env-inject-example2/build.prop"
counter=1
while IFS= read line
do
# display $line
echo "Text read from file: $line"
counter=`expr $counter + 1`
name=$(cat "$file")
echo $name
echo "Change values in config.xml..."
done <"$file"
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<config>
<builders>
<hudson.tasks.Shell>
<command>$name</command>
</hudson.tasks.Shell>
</builders>
</config>
EOF
echo "Done."
筆記:現在我已經在 shell 腳本中使用了 config.xml 來更改欄位值,但我想在 config.xml 檔案外部使用 shell 變數。
答案1
sed "s@PatternThatShouldBeReplaced@$name" /Path/To/config.xml
注意:sed 分隔符號通常是 / 但我建議在這種情況下使用 @ 來允許變數包含 / 而不轉義