Check XML content in bash

I can not plunge into this problem and I hope for your help.

In a bash script, I use the curl command, which should receive data from the server. The curl response is put into the bash variable because I want to check the answer.

The answer is as follows:

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

An important part:

 <dp:result> OK </dp:result> 

How can I check in my bash script whether this exact string is in a variable (or not)? I tried several approaches with different types of screens, but so far I have not been able to make them insignificant (always ending with a problem with one of the special characters).

Thank you for your help!

+7
variables bash shell curl
source share
1 answer

Try this using the right tool to work properly:

Team:

 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()' 

Output:

 OK 

Finally:

 #!/bin/bash result="$(chosen_command)" if [[ $result == *OK* ]]; then echo "OK" else echo >&2 "ERROR" exit 1 fi 

(replace chosen_command with ... your chosen command)

+4
source share

All Articles