syntax error in shell script

Hi

There is a syntax error in the following shell script. I must have accidentally modified it as I didn't write it:

./check.sh: line 8: syntax error near unexpected token `done'
'/check.sh: line 8: `  done < CHECKSUMS

thanks for your help
while read sum blocks name
  do
    if [ "$sum" == "$(sum $name | cut -f1 -d" ")" ] ; then
       echo sum of $name is correct!
    else
       echo sum of $name is incorrect!
    fi
  done < CHECKSUMS

Open in new window

andiejeAsked:
Who is Participating?
 
woolmilkporcCommented:
Hi again,

looks good, basically!

Could it be that there are irregular characters in the "CHECKSUMS" inputfile?

Could you post (part of) it here?

Or did you transfer the script between machines, corrupting it some way?

In the latter case you could try to rewrite it from scratch (it's not that long, is it?)

wmp
0
 
farzanjCommented:
In you if condition, either use [[  ]] instead of [ ]

OR use eq instead of ==


Second, you cannot use ! without quoting it
You have to write
echo "sum of $name is correct!"
0
 
farzanjCommented:
Can you simply get rid of !

Or escape it

echo  "\!"

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
farzanjCommented:
Looks like it is accepting [ ] as well or at least the version I am using.  However ! is a problem
0
 
edster9999Commented:
The problem is the file 'CHECKSUMS'
you are passing one line that is not in the expected format.
I'd take a stab in the dark that you have gained an extra linefeed on the end of the file.  maybe by importing it from a windows machine.

This script seems to have been used on a few pages (most of which claim they wrote it).  A quick google will show other people who have had similar issues.

Have a look at :
http://experts-answer.net/2011/01/25/shell_script___unexpected_operator/#more-8280

and go for changing line 3 to this (note the single quotes) :
if [ "$sum" == "$(sum $name | cut -f1 -d' ')" ] ; then
0
 
andiejeAuthor Commented:
the file checksums has been downloaded by ftp and unzipped. I've checked the file with a perl script which looks for non printing characters and it ends in a single \n

However i never though to check the script itself. It looks like i have given every line a carriage return and a line feed?

while read sum blocks name^M$
  do^M$
    if [ $sum -eq $(sum $name | cut -f1 -d" ") ] ; then^M$
       echo sum of $name is correct!^M$
    else^M$
       echo sum of $name is incorrect!^M$
    fi^M$
  done < CHECKSUMS^M$

Open in new window

0
 
underskyCommented:
done < CHECKSUMSwhat is this?

you send CHECKSUMS into done O_o?

i more work with bash.. but i don't think that send chars into "done" is right thing..

btw if strange also, but let this part to SH scripters..
0
 
andiejeAuthor Commented:
thanks again wmp for giving me the script initially and helping me find out how i had messed it up this time!
0
 
woolmilkporcCommented:
You transferred it via ftp without specifying "binary" transfer mode, right?
0
 
TintinCommented:
undersky

cat file | while read line
do
   ...
done

Open in new window


is the same as

while read line
do
   ..
done <file

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.