LinuxQuestions.org
Register a domain and help support LQ
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 04-28-2012, 07:06 AM   #1
UnixNewbie91
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Rep: Reputation: Disabled
Elif Statement help


Hi I have an if/elif/else statement and no matter what I enter as the variable $TIMEFRAME i always get the else statement, even though the input is saved as the variable.

Example of my statement is here, wont put it all because its long. I have already made the functions that appear after "then".
echo "Please enter the first three letter of the month and year you require. (e.g Apr 2008) ((type quit to quit script)): "
read TIMEFRAME
echo "Thank you, you have entered $TIMEFRAME."
if [ $TIMEFRAME = "May 2007" ] 2> /dev/null
then May07
elif [ $TIMEFRAME = "May 2007" ] 2> /dev/null
then May07
elif [ $TIMEFRAME = "Jul 2007" ] 2> /dev/null
then July07
elif [ $TIMEFRAME = "Quit" ] 2> /dev/null
then quit
else echo "Please try again."
fi

Any help would be appreciated
 
Old 04-28-2012, 07:12 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Since the variable may contain spaces, use double quotes when evaluating it:
Code:
if [ "$TIMEFRAME" = "May 2007" ]
 
1 members found this post helpful.
Old 04-28-2012, 07:14 AM   #3
UnixNewbie91
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank you colucix, works perfectly
 
Old 04-28-2012, 09:59 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Please mark as SOLVED if you have your solution.

I am curious though, why the redirection to null for each "if"?
 
Old 04-28-2012, 10:02 AM   #5
UnixNewbie91
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
No idea, thats how the example I was given was set out. Does it matter if I do it or not?
 
Old 04-28-2012, 10:24 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Frankly, this is the kind of situation that would do better using a case statement.

Code:
case $TIMEFRAME in

	 "May 2007")  May07	;;
	 "Jun 2007")  June07	;;
	 "Jul 2007")  July07	;;
	       Quit)  quit	;;
		  *)  echo "Please try again." ;;

esac
I'm assuming "May07" et al are function or script names, and that your doubled "May" was actually supposed to be "June".

case is for evaluating a single string for multiple possible values. if..elif..else is for evaluating multiple independent conditions in sequence.


BTW, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Last edited by David the H.; 04-28-2012 at 10:29 AM. Reason: quoting
 
Old 04-28-2012, 10:35 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Quote:
Does it matter if I do it or not?
Well the "[" is actually a function which is equivalent to the test function. If I had to guess I would say it is to handle the scenarios for when your variable is empty and the
test will throw an error. If this is the case you are either better off using [[]], as it does not suffer from this issue (assuming a bash script) or appending a superfluous character to make sure the test passes, like:
Code:
if [ "x$TIMEFRAME" == "xMay 2007" ]
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python shell help (using elif) danred Linux - General 8 10-20-2009 03:55 AM
BASH if/elif variable problem GNUJoshua Programming 6 07-15-2008 07:01 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
bash scripting question - elif ?? xscousr Programming 2 08-12-2003 09:56 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM


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