Need help fix my script to get alerts when the command produce n expected output

Hi,
Below is my script, which is used to invoke a test using curl command.

#/usr/bin/sh

MAILTO=user@xyz.com
 
URL='https://myserver.xyz.net/test/dir/test123.tws'
SOAPFILE=/tmp/soap.txt

curl -k -s -S --header 'Content-Type: text/xml;charset=UTF-8' --data @"${SOAPFILE}" "${URL}" 
 

if [ $? -eq 0 ]; 
then  mailx -s "SUCCESS " $MAILTO ; 
else mailx -s "Failure " $MAILTO; 
fi

and

expected output from the script:

1) If it is successful

<?xml xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xmlns="http://dir/text123.tws"><response>MY test is  successful</response><xxxxxxxxxxxxxxxxxxxxx>



2) other than successful

a) <?xml xxxxxxxxxxxxxxxxxxxxxxxxx:Fault xmlns:axis2ns1="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>xxxxxxxxxx1:Server</faultcode><faultstring>Internal Error</faultstring><detail></detail></soapenv:Fault><xxxxxxxxxxxxxx>

b)  Virtual Host to handle xxxxxxxxxxx.tws has not been defined.</H1><Bxxxxx5E: A  Virtual Host to handle server.xyz.com has not been defined.</H3><BR><I  Server</I>

The exit code on this script is always "0".
Even if i receive any of the above 3 output. (success/error/not defined etc)

My requirement is,
I would like to receive an email notification when the command (cURL) in the script fails. (**other than success )
please provide your input, so that i can receive alerts when the command in the script give output other than success.

If I - having no clue of curl - interpret your post correctly, what you call "expected output" is what curl writes to stdout after downloading it successfully from the server. So its exit code will always be 0.
You might want to analyse curl 's stdout for a decision "other than success".

Thanks RudiC for your response.
I am actually looking into the cURL options. Usually after every execution of my script, it produce some kind of response like i mentioned in the above post.

I'm thinking to redirect/save the output response in to a file and grep "exclude success" thing, and send an email.

But i do not think its a good idea. some times output file is not getting updated when there is failure/unsuccessful situation. In that case, it is reading the old output response.
am still researching.

This is just a new thing, which am trying to implement.
Please let me know if you guys have any ideas.

Thank you.

Do you want to distinguish between individual "other than successful" cases or would just "failure" suffice? Do single keywords in a line (or other features) identify a success or a failure?
This might point you in a direction:

RESARRAY=(SUCCESS FAILURE)
curl -k -s -S --header 'Content-Type: text/xml;charset=UTF-8' --data @"${SOAPFILE}" "${URL}" | awk '/success/ {XCOD=0} /[Ff]ault/ {XCOD=1} 1; END {exit XCOD}'
mailx -s ${RESARRAY[$?]} $MAILTO
1 Like