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 |
1. please use code tags https://www.linuxquestions.org/quest...do=bbcode#code
2. just reverse the logic OR in this case as its a binary check and you don't do anything anyway, just drop the first if [ ] Code:
#!/bin/bash See the following for a comprehensive tutorial & references/examples http://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ |
Thanks. I probably should have used this for an example instead. Thanks for the links.
I will check them out. Code:
#!/bin/bash |
Maybe I am missing something,. but it seems as if the true section doesnt need to be there.
Code:
#!/bin/bash |
Quote:
Code:
var1=2 # not known before script is run. Thanks chrism01 and szboardstretcher, marking solved and will read some more tutorials. :hattip: |
One more question.
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. |
Quote:
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 ... HTH Have a good read of those docs :) |
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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 Code:
if (( var1 > var2 )); then http://mywiki.wooledge.org/BashFAQ/031 http://wiki.bash-hackers.org/commands/classictest http://wiki.bash-hackers.org/syntax/...nal_expression http://wiki.bash-hackers.org/syntax/arith_expr |
All times are GMT -5. The time now is 04:23 PM. |