cancel
Showing results for 
Search instead for 
Did you mean: 

Shell Script help

dvorak
Moderator
Moderator
Posts: 29,502
Thanks: 6,627
Fixes: 1,483
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.
Customer / Moderator
If it helped click the thumb
If it fixed it click 'This fixed my problem'
3 REPLIES 3
paulh
Rising Star
Posts: 1,283
Thanks: 10
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)
Peter_Vaughan
Grafter
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
paulh
Rising Star
Posts: 1,283
Thanks: 10
Registered: ‎30-07-2007

Re: Shell Script help

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