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 |
Since the variable may contain spaces, use double quotes when evaluating it:
Code:
if [ "$TIMEFRAME" = "May 2007" ] |
Thank you colucix, works perfectly
|
Please mark as SOLVED if you have your solution.
I am curious though, why the redirection to null for each "if"? |
No idea, thats how the example I was given was set out. Does it matter if I do it or not?
|
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 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. |
Quote:
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. |