jto jto - 1 year ago 61
Bash Question

Shell Script won't fail in Jenkins

I have a simple shell script which I want to set up as a periodic Jenkins job rather than a cronjob for visibility and usability for less experienced users.

Here is the script:

#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt

if [ "grep -sq 'Unable' $outputfile" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi


My script will always return "NO ERROR MESSAGES HAVE BEEN FOUND" regardless of whether or not 'Unable' is in $outputfile, what am I doing wrong?

I also need my Jenkins job to class this as a success if 'Unable' isn't found (e.g. If script returns "0" then pass, everything else is fail)

Answer Source

Execute the grep command and check the exit status instead:

#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
grep -sq 'Unable' $outputfile
if [ "$?" == "0" ]; then
    echo -e "ERROR MESSAGE FOUND\n"
    exit 1
else
    echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
    exit 0
fi
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download