如何在shell腳本中根據屬性檔值取得config.XML欄位值?

如何在shell腳本中根據屬性檔值取得config.XML欄位值?

我有一個名為 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 分隔符號通常是 / 但我建議在這種情況下使用 @ 來允許變數包含 / 而不轉義

相關內容