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'.] |
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 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" ] |
Thanks gnashley
I must leave for the gym now. I will attend this later in the day when I come back home. |
Quote:
Code:
if [ "$#" -ne 2 ] |
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. |
Place your code in between [code ] and [/code ] (without the spaces).
|
Nylex
I will try immediately. |
Code:
|
|
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 ] |
Dive
Now I change it as follows: #if [ $ -ne 2] I am getting the same old error message. |
Quote:
That's because it's still commented out? Cheers, Tink |
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 ] |
Quote:
@Gins,also check your: Code:
if[-e "$to" ] |
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 |
All times are GMT -5. The time now is 03:02 AM. |