variable getting results from a log file; compare it in an IF cycle

Hi All

I am going crazy trying to resolve this easy task....

I have a log file that is created by xcode; and it contains the results of a build (either success or fail); what i am trying to achieve here is to get the result from the log, and check if is a pass or fail in an IF statement, so if it pass i write in anotehr log the build number, the date and the pass, otherwise i write in the log the fail and also send an email to myself.

the part that is causing me troubles is the extrapolation of the results and the comparison in the IF cycle:

-i tried to grep for the word FAIL in the logs, and save the results in a variable, but this does not work all the times, since i get from grep the entire line where the failure happens, while i just would like to have a bool that tells me "yes, the log has the word FAIL" or "no, the log has no FAIL" (in which case i consider it as pass).

-when i do the if statement; the result is not saved in another variable (it gets empty)

This is what i have so far in code:

checking if the logs has FAIL or PASS

IS_FAILED=`grep -r 'FAILED' ~/build_results.txt`

IF cycle

if [ -ne $IS_FAILED ]; then
        RESULT='pass'
        echo "is a pass"
else
        RESULT='fail'
        echo "is a fail"
fi

of course this won't work; and i don't get why the RESULT variable is empty, even if the echo prints the pass or fail; and i do not get how can i take just the "FAILED" string and compare it with "FAILED" in the IF cycle, instead of doing just a check to see if is empty or not (prone to errors)

is there any other way to achieve this result? I would really appreciate any suggestion.

Thanks

try

if [ ! $IS_FAILED ]; then
        RESULT='pass'
        echo "is a pass"
else
        RESULT='fail'
        echo "is a fail"
fi

or

if [ $IS_FAILED = "FAILED" ]; then
        RESULT='fail'
        echo "is a fail"  
else
       RESULT='pass'
        echo "is a pass"
fi

If all you care about is whether or not there is a match, you do not need to capture the output of grep and then compare it; you can simply test its exit status directly:

if grep -qr FAILED ~/build_results.txt; then
      RESULT='fail'
else
      RESULT='pass'
fi
echo is a $RESULT

Or, perhaps, more optimistically:

result=success
grep -qr FAILED ~/build_results.txt && result=fail
echo is a $result

Regards,
Alister

Thanks for the reply!

BTW the outcome of the grep is not "FAILED" but "** BUILD FAILED **" so i had to change the comparison string, and it works fine.