LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I can't figure out the error message. (https://www.linuxquestions.org/questions/programming-9/i-cant-figure-out-the-error-message-609939/)

Gins 12-30-2007 06:39 AM

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'.]

gnashley 12-30-2007 06:47 AM

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" ]

Gins 12-30-2007 08:23 AM

Thanks gnashley

I must leave for the gym now. I will attend this later in the day when I come back home.

dive 12-30-2007 10:10 AM

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.

Gins 12-30-2007 02:50 PM

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.

Nylex 12-30-2007 03:01 PM

Place your code in between [code ] and [/code ] (without the spaces).

Gins 12-30-2007 03:10 PM

Nylex
I will try immediately.

Gins 12-30-2007 03:13 PM

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.

Tinkster 12-30-2007 04:12 PM

Look at your use of spaces and comments.

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



Cheers,
Tink

dive 12-30-2007 04:17 PM

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/

Gins 12-30-2007 05:03 PM

Dive

Now I change it as follows:

#if [ $ -ne 2]

I am getting the same old error message.

Tinkster 12-30-2007 05:29 PM

Quote:

Originally Posted by Gins (Post 3006066)
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

dive 12-30-2007 06:53 PM

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 ]

ghostdog74 12-30-2007 09:00 PM

Quote:

Originally Posted by Tinkster (Post 3006019)
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.

gnashley 12-31-2007 02:47 AM

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



All times are GMT -5. The time now is 03:02 AM.