LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   error message in training program, line22: syntax error near unexpected token 'done' (https://www.linuxquestions.org/questions/linux-newbie-8/error-message-in-training-program-line22-syntax-error-near-unexpected-token-done-4175548381/)

Mark_S 07-19-2015 05:21 PM

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?

Aia 07-19-2015 05:35 PM

Quote:

Originally Posted by Mark_S (Post 5393597)
Code:

#!/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.

Mark_S 07-19-2015 06:38 PM

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.

Aia 07-19-2015 06:50 PM

Quote:

Originally Posted by Mark_S (Post 5393616)
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.
Code:

while [ ... ]
do
  .
  .
  .
done


michaelk 07-19-2015 06:55 PM

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

http://www.tldp.org/LDP/Bash-Beginne...ers-Guide.html

Keith Hedger 07-19-2015 06:58 PM

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

Aia 07-19-2015 07:05 PM

Quote:

I did cut and paste this into here from the word document that she posts on the web sight
Word documents and web sites software process and display quotes in fancier forms that are not proper for code. Be always aware of it.

As Keith Hedger said.
You introduced new errors in your second code-post:

Not correct
Quote:

Code:

while ["looptest" = y ]
if[ "$looptest" = n]


Correct
Quote:

Code:

while [ "looptest" = y ]
if [ "$looptest" = n ]



Mark_S 07-19-2015 07:10 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.

Sefyir 07-19-2015 10:17 PM

Quote:

Originally Posted by michaelk (Post 5393621)
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

John VV 07-20-2015 03:45 PM

Quote:

Should be #!/bin/bash NOT bsh
or sh

but scripts will run without the line

Keith Hedger 07-20-2015 04:52 PM

Quote:

Originally Posted by John VV (Post 5394002)
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.

Mark_S 07-20-2015 08:27 PM

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.

Keith Hedger 07-21-2015 05:27 PM

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.

Mark_S 07-21-2015 06:23 PM

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.