Beginner
Beginner

Reputation: 2886

XML - Search for value in XML file

I have a script that uses xmlstarlet to edit a XML file using this:

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

Which results in this:

    <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>

However the problem is that IF the script is run more than once, I get the above block added again. I would like to avoid that but I am not sure how to go about it. I cant find anything on the documentation that supports conditional statements

Upvotes: 1

Views: 71

Answers (1)

letsc
letsc

Reputation: 2567

Use count

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

Upvotes: 2

Related Questions