Trouble with variable and command assignment

I have a section of a script where I want to check a log file for a certain oracle error and if there is only one error (and it is ORA-39152) then I want to email that script is complete. Otherwise email failure and log.

Somehow with this while the log only has one error and it is ORA-39152, I keep getting the failure message. Can someone look over and maybe see my foolish mistake?

counterrors=$(egrep 'ORA-' ${MLOG5} | wc -l)

if [egrep 'ORA-39152:' ${MLOG5}] && [$counterrors = 1];
then
   uuencode $MLOG $MLOG1 $MLOG2 $MLOG3 $MLOG4 $MLOG5 | mail -s "PLR ARCHIVING SUCCESSFULLY COMPLETED ${datestamp}" xxxxxxxx
   exit
else
   ((ERRORS=ERRORS+1))
   mail -s "PLR ARCHIVING ERRORS IN PRICEARCH IMPORT LOG " xxxxxx < $MLOG5
   exit $ERRORS
fi

What kind of error? Why are you essentially egreping twice. One to set the counterrs var and the other in the first part of the if statement?

1 Like

You don't put egrep in [ ] like that, and there must always be spaces between the [ ] and their contents. Also, use -eq for arithmetic comparision.

if egrep 'ORA-39152:' "${MLOG5}" >/dev/null && [ "$counterrors" -eq 1 ]

But you could do this all with one awk:

COUNT=$(awk '/ORA-/ { N++ } /ORA-39152:/ { E=1 } END { print N+0 ; exit !E }' "${MLOG5}")
ORA="$?"

if [ "$ORA" -eq 0 ] && [ "$COUNT" -eq 1 ]
then
...
else
...
fi
1 Like

the test alias `[' requires a space in before and after the comparative expression
e.i.

if [ $counterrors -eq 1 ]
1 Like

thank you. Wow one foolish mistake and one big thanks for the awk part which I never thought about

I just had to change the awk statement slightly.