?
Solved

syntax error in shell script

Posted on 2011-04-25
10
Medium Priority
?
355 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:andieje
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35462876
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 35462889
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
 
LVL 31

Expert Comment

by:farzanj
ID: 35462893
Can you simply get rid of !

Or escape it

echo  "\!"

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 31

Expert Comment

by:farzanj
ID: 35462925
Looks like it is accepting [ ] as well or at least the version I am using.  However ! is a problem
0
 
LVL 20

Expert Comment

by:edster9999
ID: 35462940
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
 

Author Comment

by:andieje
ID: 35463074
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
 
LVL 4

Expert Comment

by:undersky
ID: 35463088
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
 

Author Comment

by:andieje
ID: 35463089
thanks again wmp for giving me the script initially and helping me find out how i had messed it up this time!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35463115
You transferred it via ftp without specifying "binary" transfer mode, right?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 35463872
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question