LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Elif Statement help (https://www.linuxquestions.org/questions/linux-newbie-8/elif-statement-help-942256/)

UnixNewbie91 04-28-2012 07:06 AM

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

colucix 04-28-2012 07:12 AM

Since the variable may contain spaces, use double quotes when evaluating it:
Code:

if [ "$TIMEFRAME" = "May 2007" ]

UnixNewbie91 04-28-2012 07:14 AM

Thank you colucix, works perfectly

grail 04-28-2012 09:59 AM

Please mark as SOLVED if you have your solution.

I am curious though, why the redirection to null for each "if"?

UnixNewbie91 04-28-2012 10:02 AM

No idea, thats how the example I was given was set out. Does it matter if I do it or not?

David the H. 04-28-2012 10:24 AM

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.

grail 04-28-2012 10:35 AM

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" ]


All times are GMT -5. The time now is 05:13 AM.