LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-04-2010, 09:23 AM   #1
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
If .. then... else .. fi


I am trying to learn if , then , else statements and I can't get it correct. I have written a basic script below and from what I am reading it looks correct but it does not go to the second option (else) if the first argument is not true.

Any help would be appreciated.

Quote:
#! /bin/bash

echo "Do you want to enter the pit ?"
read answer

if [ $answer=yes ]; then

echo " Good Luck !"
else
echo " Chicken ....."
fi



PS -> This is not homework question. (Kinda sounds like one) Not even in school. Just trying to learn...
 
Old 09-04-2010, 09:31 AM   #2
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
if [ $answer = yes ]

You need the spaces to make a valid comparison.

Note: you may want to use == instead of =. It helps to avoid that sort of confusion.
 
1 members found this post helpful.
Old 09-04-2010, 10:06 AM   #3
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Original Poster
Rep: Reputation: 103Reputation: 103
Thanks !!!



white space = Root of all evil

Thanks for the help.
 
Old 09-04-2010, 10:31 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Please mark as SOLVED if you have a solution.
 
Old 09-04-2010, 10:44 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Even better in bash and ksh you might use the extended test command, with double square brackets. Among other advantages, it prevents logic errors when the value of the variable is null and you missed the double quotes around it. Example:
Code:
if [[ $answer == yes ]]
then
  echo something
fi
For a discussion about the extended test command, you can take a look at the Advanced Bash Scripting Guide, here.
 
Old 09-04-2010, 02:47 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by jay73 View Post
if [ $answer = yes ]

You need the spaces to make a valid comparison.

Note: you may want to use == instead of =. It helps to avoid that sort of confusion.
In some shells that will produce something along the lines of '[: ==: unexpected operator'. Generally, tests should be written '[ foo = bar ]' or (better), if the shell is capable, as colucix says, '[[ foo == bar ]]'.
 
Old 09-04-2010, 05:15 PM   #7
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
No doubt. But even the "echo" command is not as portable as many seem to assume; for maximum portability C-like printf should be used...
 
  


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




All times are GMT -5. The time now is 03:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration