LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2010, 09:32 AM   #1
jchmski
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Rep: Reputation: 0
Syntax error near unexpected token


I'm fairly new to shell scripting and am having the hardest time figuring out why this simple script is giving me an error

#! /bin/sh

printf "Confirm (y/n): "
read confirm
if [ "$confirm" = "y" ]
echo "confirmed"
else
echo "not confirmed"
fi

the error is "syntax error near unexpected token `else'

I'm using TextEdit on OSX (sorry I'm limited to this). The only thing I can think of is that it's reading some return character and getting thrown off. I've tried saving the script using different encoding types but the same error pops up.

Note: I also get an error at line 2 because it does not regonize \r as a command

Any idea on what's causing this?
 
Old 06-18-2010, 09:39 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
You're missing a "; then" and also you have a type in the shebang (first line).

Code:
#!/bin/sh

printf "Confirm (y/n): "
read confirm
if [ "$confirm" = "y" ]; then
echo "confirmed"
else
echo "not confirmed"
fi
For the \r error, sounds like you have Windows end-of-line characters. Convert them to Unix (dos2unix, or in vi :set ff=dos)
 
Old 06-18-2010, 09:42 AM   #3
jchmski
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Original Poster
Rep: Reputation: 0
WHOOPS! sorry i DO have the 'then' in my script i just forgot to type it out when i posted, so it's not the 'then' that's causing it.
 
Old 06-18-2010, 10:01 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by jchmski View Post
WHOOPS! sorry i DO have the 'then' in my script i just forgot to type it out when i posted, so it's not the 'then' that's causing it.
We can't really help you unless we have full, accurate and complete information to work with - we could try to divine the answer, but that might take a while!

Please cut and paste your complete script, and also the complete error message you get when you try and execute it (which should include useful things like line numbers).
 
Old 06-18-2010, 10:05 AM   #5
jchmski
LQ Newbie
 
Registered: Jun 2010
Posts: 3

Original Poster
Rep: Reputation: 0
I figured it out. It's the ^M characters at the end of each line...

I opened vi and ran %s/^M//g and that did the trick.
 
Old 06-18-2010, 10:28 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well as John has said, not giving the whole information, such as I wrote this script in windows, does kinda make it difficult to assist.
Maybe next time
 
  


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
syntax error near unexpected token `then' snakernetb Linux - Server 4 02-16-2010 06:40 PM
sh: syntax error near unexpected token `(' venkatesh_b Linux - Newbie 1 05-16-2009 05:44 AM
Syntax error near unexpected token fi Nortekman Linux - Newbie 8 02-27-2009 03:48 PM
syntax error near unexpected token Aigarzs Linux - Newbie 3 01-12-2008 03:39 PM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 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