cancel
Showing results for 
Search instead for 
Did you mean: 

Shell Script help

Moderator
Moderator
Posts: 17,367
Thanks: 987
Fixes: 116
Registered: 11-01-2008

Shell Script help

I have a shell script:
./compilejar.sh >> jar.txt
grep -c "BUILD SUCCESSFUL" jar.txt
cnt="$1"
echo ${cnt}
if [[ "$cnt" -eq 0 ]]; then
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!BUILD FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo JAVA COMPILE FAILED
exit
fi
however it always seem to be getting caught even when the echo of cnt is 1, denoting as succesful build.
Can anyon offer any advice as to what might be wrong as i can't see it.
Thanks a lot.
Will Moderate For Thanks
3 REPLIES
Community Veteran
Posts: 1,229
Thanks: 1
Registered: 30-07-2007

Re: Shell Script help

I think you're meaning to set $cnt != 0 if grep finds the string. but at the moment you're not testing the exit code from grep so isn't cnt set on every run so it will != 0 ?
(i can't program for toffee by the way so i'm likely talking out of my fundament)
Community Veteran
Posts: 14,469
Registered: 30-07-2007

Re: Shell Script help

First $1 does not give you the output from the grep command.
$? will give you the return code (not the output) from the last command. In the case of grep, it will return 0 if it found a match and 1 if it does not.
So your code should be:
./compilejar.sh >> jar.txt
grep -c "BUILD SUCCESSFUL" jar.txt >/dev/null
if [ "$?" -eq 1 ]; then
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!BUILD FAILED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo JAVA COMPILE FAILED
exit
fi
Community Veteran
Posts: 1,229
Thanks: 1
Registered: 30-07-2007

Re: Shell Script help

wow, does that mean i was at least somewhere near the right track Peter?