[SOLVED] error message in training program, line22: syntax error near unexpected token 'done'
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
error message in training program, line22: syntax error near unexpected token 'done'
I don't know if this is a typo on my teacher's assignment or if I'm doing something wrong. I'm taking a linux class by web (middlesex community college) and we're up to the case decision section of it. I've type in this latest one
______________________
#!/bin/bsh
# using case decision logic
looptest=y
while [ "$looptest" = y ]
doifor
echo “The colors will be blue, yellow, red, orange, and so on…. ”
echo -e "Try to guess my favorite color: \c"
read guess
case “$guess” in
"blue") echo "As in My Blue Heaven.";;
"yellow") echo "As in the Yellow Sunset.";;
"red") echo "As in Red Rover, Red Rover.";;
"orange") echo "As in Autumn has shades of Orange.";;
*) echo "Sorry, I do not know that color.";;
esac
echo -n "Continue? (y)es or (n)o: "; read looptest
if [ “$looptest” = n ]
then
echo “Thank you for guessing color!”
fi
done
_____________________________________________
On both the telnet centos (using VI) remote desktop that we have to use and my own mac terminal (yosemite) and gotten the same error message.
colors: line22: syntax error near unexpected token 'done'
I thought it might be a left over line return but I've retyped the entire thing and made sure not to hit enter after done, just escape to get back into command mode and then shift ZZ to save and exit.
Am I just making a rookie mistake here?
#!/bin/bsh
# using case decision logic
looptest=y
while [ "$looptest" = y ]
doifor
echo “The colors will be blue, yellow, red, orange, and so on…. ”
echo -e "Try to guess my favorite color: \c"
read guess
case “$guess” in
"blue") echo "As in My Blue Heaven.";;
"yellow") echo "As in the Yellow Sunset.";;
"red") echo "As in Red Rover, Red Rover.";;
"orange") echo "As in Autumn has shades of Orange.";;
*) echo "Sorry, I do not know that color.";;
esac
echo -n "Continue? (y)es or (n)o: "; read looptest
if [ “$looptest” = n ]
then
echo “Thank you for guessing color!”
fi
done
Take a look at the red parts. Those are not proper quotes, it should be " and " and not and
Also ifor, what's that?
Are you using a word processor or copying parts from a website or processed document? These quotes are not done by vi.
The doifor does have me puzzled and I think that is a typo, but what do you mean
it should be " and "
Aren't " and " the same quotation marks?
I did cut and paste this into here from the word document that she posts on the web sight, but I've found it impossible to cut and paste into or out of the remote desktop we use. It is however done in vi, typed exactly as shown. I can copy it from the mac terminal window I've tried it on.
#!/bin/bsh
# using case decision logic
looptest=y
while ["looptest" = y ]
doifor
echo "The colors will be blue, yellow, red, orange, and so on..."
echo -e "Try to guess my favorite color: \c"
read guess
case "$guess" in
"blue") echo "As in My Blue Heaven.";;
"yellow") echo "As in the Yellow Sunset.";;
"red") echo "As in Red Rover, Red Rover.";;
"orange") echo "As in Autumn has shades of Orange.";;
*) echo "Sorry, I do not know that color.";;
esac
echo -n "Continue? (y)es or (n)o: "; read looptest
if[ "$looptest" = n]
then
echo "Than you for guessing color!"
fi
done
~
and when I try to run it using the sh command I get this
Marks-G5:~ Mark$ sh colors
colors: line 19: syntax error near unexpected token `then'
colors: line 19: ` then'
Marks-G5:~ Mark$
which is the same error message I get from the telnet centos system. Truth to tell I'm confused.
Oh, one thing I forgot is that after I type it in and save it I use chmod u+x to make it an executable.
The doifor does have me puzzled and I think that is a typo, but what do you mean
it should be " and "
Aren't " and " the same quotation marks?
In your original post, if you look closer you have two distinctive set of quotes. Some parts are double quoted with straight quotes, like it should be, and other parts are double quoted with fancy tilted quotes, which I highlighted in red. In your latest post, those type of quotes are gone, since you are posting from the vim/vi editor (I can see the trailing ~ at the end)
There's still the issue of the doifor. Edit it to be just do
It should be a do, since it is part of the while loop; do; done construct.
Looks like your teacher did make a typo when posting the assignment. There lots of bash programming guides on the internet that can assist you with your assignments. If you put program within code tags your indentation if any is actually used will be maintained.
The correct syntax for a while loop is:
Code:
while [ expression ]
do
statements
done
I believe Aia was trying to explain there are three different types of quotes or ticks i.e. ', " and ` (back tick) which are used in different ways. Hopefully your teacher really did not use a ` as highlighted in red.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152
Rep:
Should be #!/bin/bash NOT bsh
Must have a space between 'if' and '[' and in fact between any square bracket and the prev/next character.
No such command as 'doifor' should be 'do' to match with the later 'done'
Please use code tags it makes it easier to debug.
Indent your code, makes it easier to read eg
Code:
if [ 1 = 2 ];then
echo "somthing"
fi
Sorry you posted as I was typing
Last edited by Keith Hedger; 07-19-2015 at 07:00 PM.
Thanks all, I've got to get some sleep now to get up for work tomorrow, but I'll be working on this during the breaks and I'll let you know what happens.
Welcome to LinuxQuestions
I believe Aia was trying to explain there are three different types of quotes or ticks i.e. ', " and ` (back tick) which are used in different ways. Hopefully your teacher really did not use a ` as highlighted in red.
Not exactly. This is a common problem when copying code from websites (especially wordpress in my experience)
To demonstrate:
Code:
echo “hello”
echo "hello"
“hello”
hello
The first will come up with “hello” and the second as hello when copied into terminal.
Why? The first are not ASCII quotes but a special "smart" quote.
If the script depends on features of bash to be quoted, this can quickly break it since it also introduces two new characters as well
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152
Rep:
Quote:
Originally Posted by John VV
or sh
but scripts will run without the line
This is certainly true but I dont think its a good idea to muddy the waters with this as the OP has enough synatx errors in the script as it is, also of course using sh instead of bash alters how bash runs, sh puts it in a compatability mode and ignores a number of bash only functions, so as the script is obviously meant to be /bin/bash its best to correct that, of course for such a simple script it wont make any difference, and again this is irrelevent to the OP's problem.
Thanks, I had a few minutes to work on it tonight and with the corrections you suggested it's almost working. I get a -bash: n: command not found when I input the n when it ask me to continue or not, but I'll work on it tomorrow. Glad you responded, my prof hasn't yet. When I re-type this into the terminal program I'll cut and paste so you can see all of the changes and make any more suggestions.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152
Rep:
There are some problems with this bit, first syntax is wrong you MUST have a space both before and after the square brackets, also if looptest is not set you will get another error so change' this:
Code:
echo -n "Continue? (y)es or (n)o: "; read looptest ...
To:
Code:
echo -n "Continue? (y)es or (n)o: "; read looptest
if [ "X$looptest" = "Xn" ];then
echo "Thank you for guessing color!"
fi
The two X's cancel and prevent syntax/nil variable errors in the test and as you can see there are spaces after the if an around the square brackets, the exception being the closing ']' you dont need a space if you are using ';' to seperate two commands as it is the same as a newline.
Well I got it to work on the telnet cents, but not in the mac terminal. Strange. But here is what I ended up setting it too.
#!/bin/bash
# using case decision logic
looptest=y
while [ "&looptest" = y ]
do
echo "The colors will be blue, yellow, red, orange, and so on..."
echo -e "Try to guess my favorite color: \c"
read guess
case "$guess" in
"blue") echo "As in My Blue Heaven.";;
"yellow") echo "As in the Yellow Sunset.";;
"red") echo "As in Red Rover, Red Rover.";;
"orange") echo "As in Autumn has shades of Orange.";;
*) echo "Sorry, I do not know that color.";;
esac
echo -n "Continue?(y)es or (n)o: "; read looptest
if [ "$looptest" = n ]
then
echo "Than you for guessing color!"
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.