Results 1 to 9 of 9
Code:
#!/bin/bash
echo $1
if [ $1=="A" ];
then echo 0 > Type.txt
else
if [ $1=="B" ];
then echo 1 > Type.txt
else
if [ $1=="C" ];
then echo ...
- 04-22-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 5
My code seems to think the first if statement is always the truth!!
My code seems to always think the first if statement is true. What is wrong?Code:#!/bin/bash echo $1 if [ $1=="A" ]; then echo 0 > Type.txt else if [ $1=="B" ]; then echo 1 > Type.txt else if [ $1=="C" ]; then echo 2 > Type.txt else if [ $1=="D" ]; then echo 3 > Type.txt else echo "Fail" fi fi fi fi
- 04-22-2011 #2Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
Put a space between your "==" operator and its operands.
Lets say, for example, $1 is the empty string.
If you write if [ $1=="A" ]; without spaces between $1 and == and "A",
this is the same as writing if [ "$1==A" ];
and the string will evaluate to "==A",
which is non-null and therefore is always true.
What you want is this:Although this code would be better:Code:if [ "$1" == "A" ]: then ... else ... fi
And this code would be best:Code:if [ "$1" == "A" ] then echo 0 >Type.txt elif [ "$1" == "B" ] then echo 1 >Type.txt elif [ "$1" == "C" ] then echo 2 >Type.txt elif [ "$1" == "D" ] then echo 3 >Type.txt else echo "Fail" fi
Code:case "$1" in ("A") echo 0 >Type.txt ;; ("B") echo 1 >Type.txt ;; ("C") echo 2 >Type.txt ;; ("D") echo 3 >Type.txt ;; (*) echo "Fail" ;; esac
- 04-23-2011 #3Just Joined!
- Join Date
- Jul 2008
- Posts
- 31
FWIW, even if your test worked correctly, you'd see the same result.
if [ $1=="A" ];
The semicolon terminates the if statement, so what the compiler would see is:
if [ $1=="A" ] { };
echo 0 > Type.txt
i.e. two separate statements having nothing to do with each other other than position in the code.
- 04-23-2011 #4Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
I'm afraid you are incorrect.
Try it for yourself on the command line, enter an if statement terminated with a colon, the interpreter will expect you to enter a "then" statement, and continue prompting you until you enter a "fi" statement:Yes, semi-colons are necessary after the condition of the "if" statement if the "then" statement is on the same line. However. if there is a semi-colon before a newline, it is simply ignored, it does NOT terminate the "if" statement. In fact, not following an "if" statement with a "then" statement and then a terminating "fi" statement will cause a syntax error regardless of your use of semi-colons.Code:% a="A" % if [ "$a" == "A" ]; then echo YES; else echo NO; fi YES % if [ "$a" == "B" ]; ... then echo YES; ... else echo NO; ... fi; NO
Its a good idea to test your explanation before posting it here. I've made that mistake myself, and posted wrong information. Now I always double check.
- 04-23-2011 #5Just Joined!
- Join Date
- Jul 2008
- Posts
- 31
Just retested it and it works that way for me. (Actually I get an "unmatched if" statement, presumably the dangling then, when I execute his original example.)
Different version of bash? Who knows? I liked your case example. I do feel that programming according to the standard is the correct way to do things, and avoids situations like we have where it works for one and doesn't for the other.
- 04-23-2011 #6Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
- 04-24-2011 #7Just Joined!
- Join Date
- Jul 2007
- Location
- Chennai, India
- Posts
- 8
I think bash only supports elif.
So the logic should be:
#!/bin/bash
echo $1
if [ $1=="A" ];
then echo 0 > Type.txt
elif [ $1=="B" ];
then echo 1 > Type.txt
elif [ $1=="C" ];
then echo 2 > Type.txt
elif [ $1=="D" ];
then echo 3 > Type.txt
else
echo "Fail"
fi
- 04-24-2011 #8Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
No, you can have nested if statements, although using "elif" would be much better, and using "case" would be better still (like I said in my first response).
Also you made the same mistake as the poster of this thread, you did NOT put a space between the "==" operator:Without a space, the "==" operator is considered to be part of the string "A" (so bash sees "==A"), and because the string in the conditional is non-null, the conditional always evaluates to true.Code:#Incorrect: if [ "$1"=="A" ]; then echo YES; else echo NO; fi #Correct: if [ "$1" == "A" ]; then echo YES; else echo NO; fi
- 04-24-2011 #9Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
It's also good practice to always put your positional parameters in quotes to avoid syntax errors if someone enters an argument with embedded spaces.


Reply With Quote
