[SOLVED] bash: syntax error near unexpected token `else'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
@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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.