Shell scripting syntax error in if then else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting syntax error in if then else
# 1  
Old 07-06-2018
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.

Code:
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?

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

Thanks.

Last edited by RudiC; 07-07-2018 at 07:46 AM..
# 2  
Old 07-07-2018
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-07-2018
Quote:
Originally Posted by duke0001
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.

Code:
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

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.
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax error C shell

Hello, I have a newbe syntax error but I cant find it syntax error: unexpected end of file #!/bin/csh # pe request #$ -pe mpi_16 32 #### 16 core : 'mpi_16 16' || 24 core : 'mpi_24 24 ' # our Job name #$ -N test2MD #$ -S /bin/sh (1 Reply)
Discussion started by: dulceC
1 Replies

2. Shell Programming and Scripting

Shell scripting syntax

mydate ="$(date)" what is the use of adding double code and parenthesis in the above code .. can i write the same code as mydate= $date (5 Replies)
Discussion started by: lobsang
5 Replies

3. Shell Programming and Scripting

SHELL: syntax error near unexpected token `else'

Hi, I wrote this shell script to be a tool for my team to do daily routine works. Though, option a and b are working fine, option c is giving below error. line 647: syntax error near unexpected token `else' line 647: `else' Here s my script. A bit lengthy though. Pls excuse. Also,... (3 Replies)
Discussion started by: Naga06
3 Replies

4. Shell Programming and Scripting

Compare two initoras shell script syntax error

Hi All, I am executing the below shell script to compare two initoras files which is ending up in syntax error as below: -bash-3.2$ cat compare_two_initoras.sh #!/bin/bash file1=$1 file2=$2 for parameter in `cat $file1 | grep = | awk '{print $1}' | grep -v '#'` do value1=`grep... (4 Replies)
Discussion started by: a1_win
4 Replies

5. Shell Programming and Scripting

syntax error in shell script

I am creating a shell script. In which, I need to get server name and server IP. I used this command in script. servername=`cat /etc/hosts|grep `eval hostname`|awk '{print $2}'` however, when execute script or put set -x to debug, it return: line 13: syntax error at line 13: `|' unexpected... (4 Replies)
Discussion started by: duke0001
4 Replies

6. Shell Programming and Scripting

Syntax Error in Unix Shell Script

I am trying to run a unix script in my home directory.Snippet below echo "`date '+%Y%m%d_%H%M%S'` Getting ProductList.dat" if ( -f $DIR/ProductList.dat) then cp $DIR/ProductList.dat MigratedProductList.dat else echo "`date '+%Y%m%d_%H%M%S'`ProductList.dat does not exist; Processing... (4 Replies)
Discussion started by: Mary James
4 Replies

7. Shell Programming and Scripting

syntax error in shell test: unknown operator

Hi All, can some one figure out the syntax issue here. How to overcome this? #!/bin/sh $ HFR_MAIL=NO $ PRP_MAIL=NO $ MC_MAIL=NO $ if && && ]; then > echo "NO " > else > echo "YES" > fi test: unknown operator NO $ if && && ]; then > echo "NO" > else > echo "YES" >... (4 Replies)
Discussion started by: shellscripter
4 Replies

8. Shell Programming and Scripting

Help on shell script : syntax error at line 62: `end of file' unexpected

Hi All, I have written a korn script (code pasted below). It is giving the error while debugging "new.sh: syntax error at line 62: `end of file' unexpected". I have re-written the whole code in VI and explored all help related to this error on this Unix forum and tried it. Somehow, I could... (7 Replies)
Discussion started by: schandrakar1
7 Replies

9. UNIX for Dummies Questions & Answers

Shell Syntax Error when copying files

Hello, I apologize if this was addressed in a previous post. I have done some searching but may have missed it. I am trying to read a list from a file, for example: 3bik 3bix 3biu 3bin 1nwn and using this list, copy files with these names (ex: 3bik.dssp.Z) to a seperate folder,... (1 Reply)
Discussion started by: InfoSeeker
1 Replies

10. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies
Login or Register to Ask a Question