[SOLVED] Is the true statement necessary in a simple script?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is the true statement necessary in a simple script?
Is there a proper way to not have to say true?
If the first if is true, then no commands are needed and
continue on after the if-fi section with the rest of the script.
#!/bin/bash
var=1
if [ $var -eq 1 ]; then
true
elif [ $var -ne 1 ]; then
echo Exiting
exit
fi
echo Continuing
I tried this, and it does not work:
#!/bin/bash
var=1
if [ $var -eq 1 ]
elif [ $var -ne 1 ]; then
echo Exiting
exit
fi
echo Continuing
Thanks. I probably should have used this for an example instead. Thanks for the links.
I will check them out.
Code:
#!/bin/bash
var1=2 # not known before script is run.
var2=1
if [ $var1 -gt $var2 ]; then
true
elif [ $var1 -le $var2 ]; then
echo Exiting
exit
fi
echo Continuing
Maybe I am missing something,. but it seems as if the true section doesnt need to be there.
That is what the comment was for. I should have explained it better. I put in "2" for test purposes but it is really unknown and found before this part of the script.
Code:
var1=2 # not known before script is run.
Ok, I see what you are saying now. If it is not "that", then there is no other choice that it must be "this". I guess I am wanting a more absolute test even though it is overboard and unnecessary.
Thanks chrism01 and szboardstretcher, marking solved and will read some more tutorials.
In my examples, and assuming there are multiple elifs, if the first "if" statement is true, the if-fi completes and does not need to go on to check the rest of the elif statements. Wouldn't this make the whole section more efficient?
And in this case it looks like the "true" statement would be necessary because the "if" must have a command.
Last edited by mackes; 08-27-2012 at 11:48 PM.
Reason: if multiple elifs
assuming there are multiple elifs, if the first "if" statement is true, the if-fi completes and does not need to go on to check the rest of the elif statements.
'true' ... that's basic logic
Like I said, you would just reverse or otherwise amend the logic to avoid doing pointless tests ie doing a test that caused it to 'do nothing'.
If you really want to do that test and then do 'nothing' it looks like
[code]
if [[ some_test_here ]]
then
:
fi
[code]
':' is the do nothing but effectively do a true (null) thing. You almost never see this technique used unless the succession of if .. elif .. elif .. else .. fi tests is sufficiently long/complex that it makes sense (easier to read) if it is there, in which case (sic) consider using a case statement instead ...
chrism01's last post is on the money. elif is only needed when you have multiple separate independent conditions to test. If all you have is a boolean true/false condition, then you only need if..else; the else commands will always execute when the if part of the test returns false.
And when you have a single input, but multiple patterns to match, a case statement is usually cleaner and more efficient.
Code:
case $var in
0) echo "variable is equal to zero" ;;
1) echo "variable is equal to one" ;;
2) echo "variable is equal to two" ;;
*) echo "variable is not one, two, or three" ;;
esac
Finally, when using bash or ksh, it's recommended to use [[..]] for string/file tests, but ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.
Code:
if (( var1 > var2 )); then
echo "variable one is greater than variable two."
elif (( var1 == var2 )); then
echo "variable one is equal to variable two."
else
echo "variable one is less than variable two"
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.