I am unable to wrap my head around this issue and hope for your help.
In a bash script I use a curl command which should get data from a server. The curl response is being put into a bash variable, because I want to check the response.
The response looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body><dp:response xmlns:dp="http://www.datapower.com/schemas/management">
<dp:timestamp>2018-02-28T13:31:36+01:00</dp:timestamp>
<dp:result> OK </dp:result>
</dp:response>
</env:Body>
</env:Envelope>
The important part is:
<dp:result> OK </dp:result>
How can I check within my bash script whether this exact string is in the variable (or not)? I tried several approaches with different kind of escapes, however, failed miserably so far (always ending up with an issue with one of the special characters).
Thank you for your help!
Try this, using the proper tool for the right job :
curl ..... | xmllint --xpath '//*[local-name()="result"]/text()'
or
xmllint --xpath '//*[local-name()="result"]/text()' http://domain.tld/path
or
curl ..... | xmlstarlet sel -t -v '//*[local-name()="result"]/text()'
OK
#!/bin/bash
result="$(chosen_command)"
if [[ $result == *OK* ]]; then
echo "OK"
else
echo >&2 "ERROR"
exit 1
fi
(replace chosen_command
by... your chosen command)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With