ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'.]
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):
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.