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? |
Quote:
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. |
Quote:
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. Code:
while [ ... ] |
Welcome to LinuxQuestions
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 ] http://www.tldp.org/LDP/Bash-Beginne...ers-Guide.html |
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 |
Quote:
As Keith Hedger said. You introduced new errors in your second code-post: Not correct Quote:
Quote:
|
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.
|
Quote:
To demonstrate: Code:
echo “hello” 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 |
Quote:
but scripts will run without the line |
Quote:
|
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.
|
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 ... Code:
echo -n "Continue? (y)es or (n)o: "; read looptest |
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 Thanks for the help. |
All times are GMT -5. The time now is 08:16 AM. |