Syntax error near unexpected token `else'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax error near unexpected token `else'
# 1  
Old 07-30-2016
Syntax error near unexpected token `else'

Hello every one!!

I don't know where I am going wrong but I am finding it difficult to clear this error of syntax error near unexpected token `else'

I am writing a simple shell script to find a file in a directory and if found execute that else return an error to the log file

Code:
path=path/of/file
if [ -f "${path}"/file_name ]; then
echo "file found" >> output/file/path
else
echo "file not found" >> output/file/path
fi

Please help me understand where is this script wrong

Thanks




Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 07-30-2016 at 12:35 PM.. Reason: Added (i)code tags (again!).
# 2  
Old 07-30-2016
Hello masubram,

Please use code tags as per forum rules for your commands/Inputs/codes into your posts.
Could you please try following and let me know if this helps you.
Code:
Input_file="/path/of/file/file_name"
Output_file="/output/file/path/output_file_name"
if [[ -f $Input_file ]]
then
     echo "file found" >> $Output_file
else
     echo "file not found" >> $Output_file
fi

Thanks,
R. Singh
# 3  
Old 07-30-2016
Hi Ravinder,

Thanks for the reply, yes the error which I was mentioning is gone now but there is new one as below...

Code:
syntax error: unexpected end of file

I have given spaces between two square braces and a space for -f

Code:
[ [ -f  $input_file ] ]

Is there anywhere I need to check for the syntax errors in file

ThanksSmilie



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 07-30-2016 at 12:36 PM.. Reason: Added code tags (again!).
# 4  
Old 07-30-2016
Quote:
Originally Posted by masubram
Hi Ravinder,
Thanks for the reply, yes the error which I was mentioning is gone now but there is new one as below...
syntax error: unexpected end of file
I have given spaces between two square braces and a space for -f
[ [ -f $input_file ] ]
Is there anywhere I need to check for the syntax errors in file
ThanksSmilie
Hello masubram,

Please try to copy code above as it is, we shouldn't give spaces between [[, please try code as follows and let us know how it goes then.
Code:
path="/path/of/file/file_name"
if [[ -f $path ]]
then
     echo "file found" >> /output/file/path
else
     echo "file not found" >> /output/file/path
fi

In case you are getting error please mention your o.s detail too.

Thanks,
R. Singh
# 5  
Old 07-30-2016
I guess this is another reoccurrence of DOS line teminators <CR> (= \r, ^M, 0x0D) interfering with shell's keywords, e.g. then. How (i.e. with which editor) did you produce the script? Please post the output of
Code:
od -tx1c  script

.
# 6  
Old 07-30-2016
Hi Rudi,

Please find the o/p of the code

Code:
 0000000  23  21  2f  62  69  6e  2f  62  61  73  68  0d  0a  23  20  3d
          #   !   /   b   i   n   /   b   a   s   h  \r  \n   #       =
0000020  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d  3d
          =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =
0001315


Last edited by masubram; 07-31-2016 at 05:36 AM.. Reason: Added code tags (3rd time).
# 7  
Old 07-30-2016
Hello masubram,

Could you please remove carriage characters in your Input_file by doing as follows.
Code:
tr -d '\r' < Input_file > Temp_Input_file
mv Temp_Input_file  Input_file

Once above is done, you could try to run above codes and let us know how it goes then.

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Syntax error near unexpected token `('

detect_mouse_mvt.sh /home/andy/bin/detect_mouse_mvt.sh: line 4: syntax error near unexpected token `(' /home/andy/bin/detect_mouse_mvt.sh: line 4: `fh = file('/dev/input/mice')' #!/bin/bash # # fh = file('/dev/input/mice') while True: fh.read(3) print 'Mouse... (15 Replies)
Discussion started by: drew77
15 Replies

2. UNIX for Beginners Questions & Answers

Syntax error near unexpected token

Dears, While executing the below script im getting the error at line 30. Please let me know what changes to be done to fix this. test.sh: line 30: syntax error near unexpected token `done' test.sh: line 30: ` done ' #!/bin/sh # Rev. PA1 # author: eillops # date: 26-04-2018 # #... (1 Reply)
Discussion started by: Kamesh G
1 Replies

3. How to Post in the The UNIX and Linux Forums

Syntax error near unexpected token `('

I have 2 files like a.txt and b.txt and the content is as below cat a.txt 810750125 117780 /BSCSQAT4A/bscsqat4a/lib/jar/wclt_common.jar 1803152428 13300 /BSCSQAT4A/bscsqat4a/lib/jar/WFMSSupportTool.jar 2663502779 67049 /BSCSQAT4A/bscsqat4a/lib/jar/wma.jar 687942896 665272... (1 Reply)
Discussion started by: ranabhavish
1 Replies

4. Shell Programming and Scripting

Syntax error near unexpected token

Hi all, I have a simple script that doesn't work somehow. I can't seem to be spotting the cause of the malfunction. count=$((1)) for item in `cat test1.txt` printf %s `sed -n $((count))p test2.txt` > test3.txt count=$((count+1)) do something done I get ; ./why.sh: line 3:... (14 Replies)
Discussion started by: y33t
14 Replies

5. Shell Programming and Scripting

Syntax error near unexpected token '('

I tried to execute the code but I got this error ./Array.c: line 9: syntax error near unexpected token '(' ./Array.c: line 9: ' nvals = get_data(a,MAXARRAY);' and #include<stdio.h> #define MAXARRAY 1000 main() { int a, nvals; nvals =... (7 Replies)
Discussion started by: sgradywhite
7 Replies

6. Shell Programming and Scripting

Syntax error near unexpected token `else'

Hi, I am trying to read the session log through script. But it keeps showing me some error near. I have tried everything. Even tried converting the script using sed command to remove the hidden characters(\r).But nothing seems to be working.Below is the script : #!/bin/bash cd... (6 Replies)
Discussion started by: Aryan12345
6 Replies

7. Shell Programming and Scripting

Syntax error near unexpected token `('

What do I do here? #!/bin/bash payload=-1 AND 1=IF(21,BENCHMARK(5000000,MD5(CHAR(115,113,108,109,97,112))),0)# hash=`echo -n $payload md5sum tr -d 'n' sed 'ss-sg' md5sum tr -d 'n' sed 'ss-sg'` curl --data cs2=chronopay&cs1=$payload&cs3=$hash&transaction_type=rebill... (2 Replies)
Discussion started by: iiiiiiiiiii
2 Replies

8. Shell Programming and Scripting

syntax error near unexpected token `='

Hi all, This is a script which converts hex to bin. However am finding an error while executing syntax error near unexpected token `=' `($hexfile, $binfile) = @ARGV;' I am running using ./fil.pl <hexfile> <binfile> ################################################### # # this script... (3 Replies)
Discussion started by: jaango123
3 Replies

9. UNIX for Advanced & Expert Users

syntax error near unexpected token '{

Hi, I am running the following script through cygwin and getting below mentioned error. ******************************************* #!/bin/sh # constants WORK_DIR="deploy" INFOFILE="deploy.info" INTROFILE="Intro.sh" CMGMT_PKG="com.kintana.cmgmt.deploy" DEPLOY_PREFIX="mitg" ... (2 Replies)
Discussion started by: MandyR
2 Replies

10. Shell Programming and Scripting

sh syntax error unexpected token done

I'm getting the following error: line 21: syntax error near unexpected token `done` line 21: `done` and I haven't been able to figure out why. Here is my code #!/bin/sh if ; then echo 'Usage: rename getexp/replStr ' exit 0 fi arg = $1 shift while ; do (5 Replies)
Discussion started by: NullPointer
5 Replies
Login or Register to Ask a Question