LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-19-2015, 05:21 PM   #1
Mark_S
LQ Newbie
 
Registered: Jul 2015
Posts: 28

Rep: Reputation: Disabled
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?
 
Old 07-19-2015, 05:35 PM   #2
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
Originally Posted by Mark_S View Post
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.

Last edited by Aia; 07-19-2015 at 05:40 PM.
 
Old 07-19-2015, 06:38 PM   #3
Mark_S
LQ Newbie
 
Registered: Jul 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Mark_S; 07-19-2015 at 06:39 PM.
 
Old 07-19-2015, 06:50 PM   #4
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Quote:
Originally Posted by Mark_S View Post
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

Last edited by Aia; 07-19-2015 at 06:56 PM.
 
Old 07-19-2015, 06:55 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,743

Rep: Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923
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

Last edited by michaelk; 07-19-2015 at 06:57 PM.
 
Old 07-19-2015, 06:58 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
 
Old 07-19-2015, 07:05 PM   #7
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
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 ]

Last edited by Aia; 07-19-2015 at 11:04 PM.
 
Old 07-19-2015, 07:10 PM   #8
Mark_S
LQ Newbie
 
Registered: Jul 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-19-2015, 10:17 PM   #9
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by michaelk View Post
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

Last edited by Sefyir; 07-19-2015 at 10:19 PM.
 
Old 07-20-2015, 03:45 PM   #10
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,627

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
Quote:
Should be #!/bin/bash NOT bsh
or sh

but scripts will run without the line
 
Old 07-20-2015, 04:52 PM   #11
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by John VV View Post
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.
 
Old 07-20-2015, 08:27 PM   #12
Mark_S
LQ Newbie
 
Registered: Jul 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-21-2015, 05:27 PM   #13
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
 
Old 07-21-2015, 06:23 PM   #14
Mark_S
LQ Newbie
 
Registered: Jul 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Mark_S; 07-21-2015 at 06:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Getting error : syntax error near unexpected token `&' . psathiya1987 Linux - Software 1 10-08-2012 03:30 AM
script.pl with sed shell calls: sh error syntax error near unexpected token `(' MMaddoxx Programming 7 11-24-2011 08:00 AM
syntax error near unexpected token `fi': don't know how to get rid of this error. stf92 Programming 2 07-07-2011 10:40 AM
Bash Error: syntax error near unexpected token `{ araouf Linux - Newbie 1 03-04-2010 04:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration