Shell scripting syntax error in if then else

I have one bash shell script to execute table refreshment. At the bottom of script, I have one piece of code to check 'ORA-' error from log file, then send email to DBA or application people. But this piece of code didn't work. I tried different ways and also search online to find where is my syntax error. It all didn't work. So I posted my code there for help and critique.

chk_refresh=`cat $LOGDIR/Erefresh.log|grep "ORA-"` if [[ -z $chk_refresh ]];  then echo "The $ORACLE_SID ERfrsh done, NO ERRORS on $(date)"|mailx -s "Refresh" Bob.Ross@NG.com else echo "The $ORACLE_SID ERfrsh done, ERRORS. on $(date)"|mailx -s "Refresh Errors" Bob.Ross@NG.com fi

When I execute shell script, it pump up error like this: Please help to identify where is the point of error?

./Ereports_refresh_test.sh: line 30: syntax error near unexpected token `then'

Thanks.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Having said this, there's a semicolon missing in front of the if .
Here you can see how beneficial it is to wite code in a structured way, indenting blocks belonging together, in contrast to spaghetti one liners.

1 Like

I think RudiC will have discovered one basic error but i don't understand why you have opted for backticks for your chk_refresh=`cat $LOGDIR/Erefresh.log|grep "ORA-"` yet use $(date) in the "echo" part.

Also there should also be a semi-colon just before "else" and "fi"!
You really should structure your code and thus these would be picked straight up.

1 Like