LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Is the true statement necessary in a simple script? (https://www.linuxquestions.org/questions/programming-9/is-the-true-statement-necessary-in-a-simple-script-4175424326/)

mackes 08-27-2012 09:10 PM

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

chrism01 08-27-2012 09:17 PM

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

var=1
if [[ $var -ne 1 ]]
then
    echo Exiting
    exit
fi
echo Continuing

http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

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/

mackes 08-27-2012 10:11 PM

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


szboardstretcher 08-27-2012 10:24 PM

Maybe I am missing something,. but it seems as if the true section doesnt need to be there.

Code:

#!/bin/bash

var1=2    # not known before script is run.
var2=1
if [ $var1 -le $var2 ]; then
echo Exiting
exit
fi

echo Continuing


mackes 08-27-2012 10:43 PM

Quote:

Originally Posted by szboardstretcher (Post 4766056)
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. :hattip:

mackes 08-27-2012 11:37 PM

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.

chrism01 08-28-2012 01:15 AM

Quote:

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 ...

HTH

Have a good read of those docs :)

colucix 08-28-2012 10:21 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

David the H. 08-28-2012 12:31 PM

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

http://mywiki.wooledge.org/ArithmeticExpression
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.