LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-30-2007, 06:39 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
I can't figure out the error message.


#!/bin/sh

# copy a file

#

#if [ "$#" -ne 2]

then

echo "Usage: mycp from to"

exit1


fi

from="$1"

to="$2"


#

# See if the destination file already exists

#

if[-e "$to" ]



then
echo "$to already exists; overwrite (yes/no)?"

read answer

if["$answer" !=yes]

then

echo "Copy not performed"

exit 0

fi

fi


#

# Either destination doesn't exist or "yes" was typed

#



#

cp $from $to # Proceed with copy

$

--------------------------------------------------------------


Ni@linux-3vxw:~/SCRIPTING> ./mycp
./mycp: line 11: syntax error near unexpected token `then'
./mycp: line 11: ` then'
Ni@linux-3vxw:~/SCRIPTING>

I can't figure out the above error message. What is the unexpected token here?

Please help me.

[ I wrote it using the vi editor. The name of the file is 'mycp'.
I make it executable by typing ' chmod 755 mycp ' There is a folder called 'SCRIPTING'.]

Last edited by Gins; 12-30-2007 at 06:41 AM.
 
Old 12-30-2007, 06:47 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Can't you please use the CODE tags so your code is more readable. You dont even have to use the advanced posting mode -just write it like this:
Code:
 code here
in the normal quick posting method. this will make your code much easier to see.

It appears that the problem is that you need a space between 'if' and '[' and yopu also always need spaces between '[' and ']' and what is inside them. Youalso always need to use spaces between values and the = or != signs. Plus, especially when using single brackets, bot valuse should be in quotes if you are using the text operands (=, !=, <, <=, >, >=). If you are using numeric values then use the numeric operands(-eq, -ne, -lt, -le, -gt, -ge):

Code:
if [ "$answer" != "yes" ]
 
Old 12-30-2007, 08:23 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks gnashley

I must leave for the gym now. I will attend this later in the day when I come back home.
 
Old 12-30-2007, 10:10 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Quote:
#if [ "$#" -ne 2]
This line is commented out. You will get an error when bash encounters the next line 'then' as well as having no space between 2 and ].

Code:
if [ "$#" -ne 2 ]
P.S. Please don't double space your code, it makes it very hard to read.

Last edited by dive; 12-30-2007 at 10:14 AM.
 
Old 12-30-2007, 02:50 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I came home from the gym and struggled nearly half an one hour to fix this.
No it does not work.
I get the same old error message.

Gnashley asked me to post in the following manner:


Code:

code here

-------------------------------------

I want to post you in the above manner. So you could get a better idea as to the error.

How do I post the way gnashley suggeted?
Please tell me.

Last edited by Gins; 12-30-2007 at 02:51 PM.
 
Old 12-30-2007, 03:01 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Place your code in between [code ] and [/code ] (without the spaces).
 
Old 12-30-2007, 03:10 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Nylex
I will try immediately.
 
Old 12-30-2007, 03:13 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Code:
 

 #!/bin/sh

 # copy a file

 #

 #if [ "$#" -ne 2]

  then

        echo "Usage: mycp from to"

        exit1


   fi

   from="$1"

   to="$2"


 #

 # See if the destination file already exists

 #

   if[-e "$to" ]
                           

   then
          echo "$to already exists; overwrite (yes/no)?"

          read answer

          if [ "$answer" != yes] then



          echo  "Copy not performed"

          exit 0

     fi

          fi


  #

  # Either destination doesn't exist or "yes" was typed

  #
                                        

     cp  $from $to     # Proceed with copy

  $
You are great Nylex. It worked beautifully. Please try on your system to see the error message.

Last edited by Gins; 12-30-2007 at 03:15 PM.
 
Old 12-30-2007, 04:12 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Look at your use of spaces and comments.

http://tldp.org/LDP/abs/html/index.html



Cheers,
Tink
 
Old 12-30-2007, 04:17 PM   #10
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Like I said before '#if [ "$#" -ne 2]' is commented out. # = a comment line, so remove the # and put a space before the ].

Code:
if [ "$#" -ne 2 ]
Have a read through this when you get time: http://www.tldp.org/LDP/abs/html/
 
Old 12-30-2007, 05:03 PM   #11
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Dive

Now I change it as follows:

#if [ $ -ne 2]

I am getting the same old error message.
 
Old 12-30-2007, 05:29 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Gins View Post
Dive

Now I change it as follows:

#if [ $ -ne 2]

I am getting the same old error message.

That's because it's still commented out?


Cheers,
Tink
 
Old 12-30-2007, 06:53 PM   #13
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
You removed the wrong #. Lines that begin with a # are comments. And you still have no space before the ]. The correct line is:

Code:
if [ "$#" -ne 2 ]
 
Old 12-30-2007, 09:00 PM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Tinkster View Post
Look at your use of spaces and comments.

http://tldp.org/LDP/abs/html/index.html



Cheers,
Tink
don't think he has the time to read that.
@Gins,also check your:
Code:
 if[-e "$to" ]
there should be a space between the open bracket and the "-" sign.
 
Old 12-31-2007, 02:47 AM   #15
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Pay close attention to the spacing around brackets. The $ at the end was completely out of place and useless. You either need to read a lot in the bash man-pages or advanced bash guide, or begin learning by copying other peoples code and then making small changes to it until you learn the basics of the shell language.

Code:
#!/bin/sh

# copy a file
if [ "$#" -ne 2 ] ; then
  echo "Usage: mycp from to"
  exit1
fi

from="$1"
to="$2"

# See if the destination file already exists
if [ -e "$to" ] ; then
  echo "$to already exists; overwrite (yes/no)?"
  read answer

  if [ "$answer" != yes] ; then
    echo  "Copy not performed"
    exit 0
  fi
fi

# Either destination doesn't exist or "yes" was typed
cp  $from $to     # Proceed with copy

Last edited by gnashley; 12-31-2007 at 05:27 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i couldn't figure out error message hibudd Linux - Software 1 03-26-2007 10:26 AM
Can't figure out this error MrSako Linux - Software 2 01-06-2007 10:13 AM
Strange Repeating Error message in /var/log/message lucktsm Linux - Security 2 10-27-2006 08:29 AM
New to Ubuntu - can't figure out dpkg "kernel image" message for RT2500 install phaggood Ubuntu 1 02-09-2006 11:41 AM
Configuration Error can't figure out!! amrogers3 Linux - Newbie 16 01-29-2004 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration