LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 05-10-2010, 03:32 AM   #1
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Lubuntu, Slackware
Posts: 575

Rep: Reputation: 64
bash: syntax error near unexpected token `else'


Code:
for i in $(ls -1a); do 
	if	[ "$i" == '.' ] then 
		echo 'one dot'
	elif	[ "$i" == '..' ] then 
		echo 'two dots'
	else
		echo 'yay'
	fi; 
done

bash: syntax error near unexpected token `else'
Why?!!

Yes, I googled. I also searched this forum. Still stumped. How is my code any different from these examples or these examples?
 
Old 05-10-2010, 03:34 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The then parts are not placed correctly. It should be:

if [ "$i" == '.' ]
then


or

if [ "$i" == '.' ]; then

Hope this helps.
 
1 members found this post helpful.
Old 05-10-2010, 04:07 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,174

Rep: Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777
Quote:
How is my code any different from these examples or these examples?
As druuna has pointed out, all of these examples adhere to the rules mentioned.
 
0 members found this post helpful.
Old 05-10-2010, 10:15 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
I don't know what exactly you're actually trying to accomplish, but this is certainly wrong:
Code:
for i in $(ls -1a); do
See http://mywiki.wooledge.org/ParsingLs

Also, the Advanced Bash Scripting guide is not an appropriate resource to learn scripting. Use http://mywiki.wooledge.org/BashGuide instead.
 
1 members found this post helpful.
Old 05-10-2010, 11:47 AM   #5
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Lubuntu, Slackware
Posts: 575

Original Poster
Rep: Reputation: 64
Unhappy

Quote:
Originally Posted by druuna View Post
Hi,

The then parts are not placed correctly. It should be:

if [ "$i" == '.' ]
then


or

if [ "$i" == '.' ]; then

Hope this helps.
I disagree with you. There is no semi-colon before the THEN statement. Look:

Code:
luc$[518]test> for i in $(ls -1a); do
> if[ "$i" == '.' ]; then
bash: syntax error near unexpected token `then'

The correct way is without the semi-colon. But something is up with the ELSE statement:
Code:
luc$[519]test> for i in $(ls -1a); do
> if[ "$i" == '.' ] then
> echo 'one dot'
> elif[ "$i" == '..' ] then
> echo 'two dots'
> else
bash: syntax error near unexpected token `else'

luc$[520]test> for i in $(ls -1a); do
> if[ "$i" == '.' ] then echo 'one dot'
> elif[ "$i" == '..' ] then echo 'two dots'
> else
bash: syntax error near unexpected token `else'

luc$[521]test> for i in $(ls -1a); do
> if[ "$i" == '.' ] then echo 'one dot'
> elif[ "$i" == '..' ] then echo 'two dots'
> else echo 'yay'
bash: syntax error near unexpected token `else'
 
Old 05-10-2010, 11:56 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Nope. druuna's advice is correct. To put if and then on the same line you have to separate them with semi-colon, that is the same you did with for and do in the first line. The problem in the last examples is that you forgot a space between if and [ and between elif and [. This should work:
Code:
$ for i in $(ls -1a); do
> if [ "$i" == '.' ]; then echo 'one dot'
> elif [ "$i" == '..' ]; then echo 'two dots'
> else echo 'yay'
> fi
> done
 
2 members found this post helpful.
Old 05-10-2010, 12:04 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@lucmove: You confuse the command line with a script. They do not always work the same:
Code:
$ cat foobar
#!/bin/bash

for i in $(ls -1a); do 
        if      [ "$i" == '.' ]
then 
                echo 'one dot'
        elif    [ "$i" == '..' ]
then 
                echo 'two dots'
        else
                echo 'yay'
        fi; 
done


for i in $(ls -1a); do 
        if      [ "$i" == '.' ]; then 
                echo 'one dot'
        elif    [ "$i" == '..' ]; then 
                echo 'two dots'
        else
                echo 'yay'
        fi; 
done

$ ls -la
total 36
drwxr-x---  2 druuna internet  4096 May 10 19:01 .
drwxr-x--- 42 druuna internet 28672 May 10 19:01 ..
-rwxr-x---  1 druuna internet   317 May 10 19:01 foobar


$ ./foobar 
one dot
two dots
yay
one dot
two dots
yay
Both my solutions work as expected.

Hope this clears things up.

Beaten to it by colucix

Last edited by druuna; 05-10-2010 at 12:06 PM.
 
1 members found this post helpful.
Old 05-10-2010, 02:23 PM   #8
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Lubuntu, Slackware
Posts: 575

Original Poster
Rep: Reputation: 64
Ok, problem solved. The problem was I had used tab instead of space before the brackets. Thanks to all.
 
Old 05-10-2010, 08:30 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,174

Rep: Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777
@OP - So just to confirm, you are saying that the following code works?
Code:
if [ "$i" == '.' ] then
    echo 'one dot'
fi
If so, I would like to write to the authors of such pages as - http://tldp.org/LDP/abs/html/testconstructs.html
And ask them why the misleading quote below?
Quote:
When if and then are on same line in a condition test, a semicolon must terminate the if statement. Both if and then are keywords. Keywords (or commands) begin statements, and before a new statement on the same line begins, the old one must terminate.
 
0 members found this post helpful.
Old 05-10-2010, 09:03 PM   #10
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Lubuntu, Slackware
Posts: 575

Original Poster
Rep: Reputation: 64
No, that code doesn't work. My mistake has been found, the code has been corrected, the problem is solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: syntax error near unexpected token `(' Folklore Linux - Newbie 1 05-02-2010 03:35 AM
-bash: syntax error near unexpected token ty1on Linux - Newbie 3 08-26-2009 02:19 PM
syntax error near unexpected token in bash using perl kambrish Programming 21 07-03-2007 12:42 PM
Trouble with Bash -- syntax error near unexpected token `fi' anamericanjoe Programming 5 05-19-2006 02:59 PM


All times are GMT -5. The time now is 11:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration