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

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

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.

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

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

Thanks:b:

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.

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

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

od -tx1c  script

.

Hi Rudi,

Please find the o/p of the 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

Hello masubram,

Could you please remove carriage characters in your Input_file by doing as follows.

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

Hi Ravinder,

I have done as mentioned, but I am still getting the same error:confused:

Thanks,
Subbu

Did you read my entire post#5?

Sorry, I am using VI editor to produce the script

Hmmm - a bit surprising. Is possibly the fileformat(s) (or ff(s)) option set to "dos"?

Hi Ravinder,

Sorry for a late reply...ya I have tried the above quote and as I mentioned there was no use and about spaces b/w those square brackets, I tried it too but got more errors, so I restored the spaces and I believe it is the right WY to go about it as I found in numerous portals.

Anyways, appreciate your help!! :wink:

Thanks,
Subbu

On which _host_ OS?
(I am thinking of CygWin here, although IIRC 'vi' is not in the default install CygWin command layer...)

Besides, the CR character, this also shows a part of the script that is not in the sample in post #1.

Could you post the rest? I suspect the problem lies elsewhere in the script.

Just checked and 'vi' is part of the CygWin install, I thought it was 'vim', but type in 'vi' and voila up comes the editor...
Must be an 'alias' for the 'vim' executable.