我的XML:
<?xml version='1.0' encoding='UTF-8'?>
<mm2-moduleset plugin="mm2-plugin@2.27.0-SNAPSHOT">
..
<buildType class="hudson.mm.MMModuleSet$Component">
<viewableName>Component</viewableName>
</buildType>
<selectedCodeComponents>
<string>admin-content</string>
<string>admin</string>
</selectedCodeComponents>
<incrementalBuild>false</incrementalBuild>
...
...
</mm2-moduleset>
</xml>
基本上,我试图从jenkins-cli插件中获取作业详细信息,并更新xml文件的配置(jenkins作业的配置),然后使用shell脚本更新jenkins作业。
为此,我需要替换&#34; string&#34; &#34; selectedCodeComponents&#34;的属性值属性根据用户输入使用shell脚本。如果输入是检查,检查内容,银行,那么
<selectedCodeComponents>
<string>check-content</string>
<string>check</string>
<string>bank</string>
</selectedCodeComponents>
我的代码:
${JAVA_HOME}/bin/java -Djavax.net.ssl.trustStore=${JENKINS_HOST}.keystore -Djavax.net.ssl.trustStorePassword=112012 -jar ./jenkins-cli.jar -s https://$USERNAME:$API_TOKEN\@$JENKINS_HOST:$JENKINS_PORT\/jenkins get-job job_1 > job1.xml
(Jenkins-cli-get-job在xml文件中返回配置)
我想首先删除selectedcodecomponents中的字符串元素,并附加用户提供的内容。像这样,
xmlstarlet ed -d "mm2-moduleset/selectedCodeComponents/string" abc.xml
// some logic
xmlstarlet ed -a "mm2-moduleset/selectedCodeComponents/string" --type elem -n string -v "something" abc.xml
但是在执行第一个命令(xmlstarlet ed -d)之后,它也删除了seletedcodecomponents。但我想只删除其中的字符串元素。
<?xml version='1.0' encoding='UTF-8'?>
<mm2-moduleset plugin="mm2-plugin@2.27.0-SNAPSHOT">
..
<buildType class="hudson.mm.MMModuleSet$Component">
<viewableName>Component</viewableName>
</buildType>
</selectedCodeComponents>
<incrementalBuild>false</incrementalBuild>
...
...
</mm2-moduleset>
</xml>,
append命令也会附加两次。(xmlstarlet ed -a) 例如,
xmlstarlet ed -a "mm2-moduleset/selectedCodeComponents/string" --type elem -n string -v "check" abc.xml
<?xml version='1.0' encoding='UTF-8'?>
<mm2-moduleset plugin="mm2-plugin@2.27.0-SNAPSHOT">
..
<buildType class="hudson.mm.MMModuleSet$Component">
<viewableName>Component</viewableName>
</buildType>
<selectedCodeComponents>
<string>admin-content</string>
<string>check</string>
<string>admin</string>
<string>check</string>
</selectedCodeComponents>
<incrementalBuild>false</incrementalBuild>
...
...
</mm2-moduleset>
</xml>
你能告诉我如何只删除selectedcodecomponents元素中的字符串元素以及append命令追加两次的原因
我是shell脚本的新手,因此您的建议可以帮助我
答案 0 :(得分:2)
稍微修复一下示例XML文件,给它一个根标记:
$ cat file.xml
<?xml version='1.0' encoding='UTF-8'?>
<root>
<buildType class="hudson.mm.MMModuleSet$Component">
<viewableName>Component</viewableName>
</buildType>
<selectedCodeComponents>
<string>admin-content</string>
<string>admin</string>
</selectedCodeComponents>
<incrementalBuild>false</incrementalBuild>
</root>
您可以删除此类string
节点
$ xmlstarlet ed -d '//selectedCodeComponents/string' file.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<buildType class="hudson.mm.MMModuleSet$Component">
<viewableName>Component</viewableName>
</buildType>
<selectedCodeComponents/>
<incrementalBuild>false</incrementalBuild>
</root>
然后像这样添加新的字符串子节点
$ xmlstarlet ed -d '//selectedCodeComponents/string' file.xml |
xmlstarlet ed -s '//selectedCodeComponents' -t elem -n string -v something1 |
xmlstarlet ed -s '//selectedCodeComponents' -t elem -n string -v something2 |
xmlstarlet ed -s '//selectedCodeComponents' -t elem -n string -v something3
<?xml version="1.0" encoding="UTF-8"?>
<root>
<buildType class="hudson.mm.MMModuleSet$Component">
<viewableName>Component</viewableName>
</buildType>
<selectedCodeComponents>
<string>something1</string>
<string>something2</string>
<string>something3</string>
</selectedCodeComponents>
<incrementalBuild>false</incrementalBuild>
</root>
答案 1 :(得分:0)
1)如果您需要删除整个字符串元素,请使用下面的代码
xmlstarlet ed -P -S -d "/mm2-moduleset/.../selectedCodeComponents/string" config.xml >temp.xml
mv temp.xml config.xml
2)如果你必须删除特定的字符串元素,请使用下面的代码
read -p "read the string that need to be deleted" deleteString
xmlstarlet ed -P -S -d "/mm2-moduleset/.../selectedCodeComponents/string[text()='$deleteString']" config.xml >temp.xml
mv temp.xml config.xml
3)如果你必须根据用户输入替换字符串,你可以尝试下面的代码
read - "read the string that need to be modify" modifyString1 modifyString2 modifyString3
xmlstarlet ed -P -S -s "/mm2-moduleset/.../selectedCodeComponents" -t elem -n string -v $modifyString1 -s "/mm2-moduleset/.../selectedCodeComponents" -t elem -n string -v $modifyString2 -s "/mm2-moduleset/.../selectedCodeComponents" -t elem -n string -v $modifyString3 config.xml >temp.xml
mv temp.xml config.xml