Syntax error near unexpected token `done'

Hi all,
Here is a simple script that is working in one server and is giving a syntax error in other server. Can somebody help me ?

 
 
#!/bin/bash
# ftp files
done < $file

errors:

I tried..with no success:

if [ "$key" = 'RUNNING' ]; then

remove single quote and add double quote

file="/impact/prod/sqllog0.txt"

check the script for any unexpected character :slight_smile:

this should not matter - leave the single quotes.
change:

if [ $key = "RUNNING" ]; then 

to

if [ "$key" = 'RUNNING' ]; then 

Try:

cat $file | while read line
do
...
if [ "$key" = RUNNING ]; then
...
done

instead of

while read line
do
...
if [ $key = "RUNNING" ]; then
...
done < $file

No luck, I am getting the same errors :mad:

#!/bin/bash
# ftp files
done < $file

Here are the errors:

 
sql.sh: line 18: syntax error near unexpected token `done'
'ql.sh: line 18: `done < $file

You missed to remove the last "< $file".

Problem with this lines...

cat $file | while read line

done < $file

Please remove cat $file or < $file.

#!/bin/bash
# ftp files
done 

errors:

sql.sh: line 18: syntax error near unexpected token `done'
'ql.sh: line 18: `done 

Please try

if [ "$key" == "RUNNING" ]; then 

Check whether you have any control m characters in your script

cat -vet sql.sh
1 Like

Then you have a broken last line like vidyadhar85 and ambu32 already suspected.
I reproduce your problem with this:

$ tail -1 sql.sh | od -c
0000000   d   o   n   e      \r  \n
0000007
$ sql.sh
'ql.sh: line 18: syntax error near unexpected token `
'ql.sh: line 18: `done 

Hey,

I tried samething , it's working to me,, Looks your input file has some problem , pls check..

filename :kk
#!/bin/bash
DATE=`date +%Y%m%d`
file='/gsl-as2/home/kponnan/myprg/tmp.txt'
cat $file | while read line
do
STAT=${line%/*}
key=${line%/*}
echo "OK1"
echo "$STAT"
echo "$DATE"
#
if [ "$key" = 'RUNNING' ]; then
echo "EXIT, Job is running"
exit
fi
#
done

Output:

bash-3.00$ bash kk
OK1
#!/bin
20100427
OK1
DATE=`date +%Y%m%d`
20100427
OK1
file='/gsl-as2/home/kponnan/myprg
20100427
OK1
cat $file | while read line
20100427
OK1
do
20100427
OK1
STAT=${line%
20100427
OK1
key=${line%
20100427
OK1
echo "OK1"
20100427
OK1
echo "$STAT"
20100427
OK1
echo "$DATE"
20100427
OK1
#
20100427
OK1
if [ "$key" = 'RUNNING' ]; then
20100427
OK1
echo "EXIT, Job is running"
20100427
OK1
exit
20100427
OK1
fi
20100427
OK1
#
20100427
OK1
done
20100427
OK1
20100427

How to fix the imput file? It is a simple text file
chmod 777 sqllog0.txt
cat sqllog0.txt

 
stop
RUNNING

Problem is in your script not in the input file. Check whether you have any non-printable characters

cat -vet sql.sh

and how to find non-printable characters ?

cat -vet sql.sh
done^M$

You script has non-printable control-m(^M) characters

Use this command to delete the control-m characters

cat sql.sh | tr -d \\015 > sql.sh

you can use dos2unix command

see the man page for dos2unix

Great! I did cat sql.sh | tr -d \\015 > sql.sh and now the script is empty ???

Sure, that was a great way to clear that file.

Shame on anbu23 for a redirection that clobbered the file. Double shame for clobbering with an unnecessary use of cat (I'm just joking, anbu ;))

All kidding aside, always backup before testing potential solutions.

Regards,
Alister

---------- Post updated at 07:00 PM ---------- Previous update was at 06:56 PM ----------

On a side note: That pipeline can sometimes work as intended, if the contents of the file are brief and the kernel scheduler smiles upon you.