我有一个使用xmlstarlet
编辑XML文件的脚本:
sudo xmlstarlet ed -L --omit-decl \
-s '//configuration' -t elem -n "property" -v '' \
-s '//configuration/property[last()]' -t elem -n "name" -v "this.is.a.node" \
-s '//configuration/property[last()]' -t elem -n "value" -v "value.for.above.node" \
-s '//configuration' -t elem -n "property" -v '' \
-s '//configuration/property[last()]' -t elem -n "name" -v "this.is.another.node" \
-s '//configuration/property[last()]' -t elem -n "value" -v "value.for.second" \
/path/to/file
结果如下:
<configuration>
....
....
....
<property>
<name>this.is.a.node</name>
<value>value.for.above.node</value>
</property>
<property>
<name>this.is.another.node</name>
<value>value.for.second</value>
</property>
</configuration>
但问题是,如果脚本运行多次,我会再次添加上面的块。我想避免这种情况,但我不知道该怎么做。我无法在支持条件语句的文档中找到任何内容
答案 0 :(得分:2)
使用GrayLog
if [[ $(xmlstarlet sel -t -v "count(/configuration/property[name='this.is.a.node'])" /paht/to/file/xml) -eq 1 ]];then echo "there"; else echo "nope"; fi