LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with my if statement (sometimes goes to else?) (https://www.linuxquestions.org/questions/programming-9/help-with-my-if-statement-sometimes-goes-to-else-825746/)

genderbender 08-12-2010 04:51 AM

Help with my if statement (sometimes goes to else?)
 
Numbers are all specified in 4 decimal numbers, e.g 28.50. Occasionaly the following code will slip into the else section and I've no idea why, the temperature is reported correctly.
at present ok=28.00, warning=29.00 and critical=32.00. I'm getting unknown status at 28.62C. Don't really understand what's wrong with the logic... Perhaps someone could help me, this isn't bash specific so people with knowledge of if statements should get it.

Code:

DEV_TMP=`echo $DEV_TMP_DEC | tr -d "."`
warning=`echo $warning_dec | tr -d "."`
critical=`echo $critical_dec | tr -d "."`
ok=`echo $ok_dec | tr -d "."`

if [ $DEV_TMP -gt $critical ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=CRITICAL
        echo "$state $msg"
        exit 2
elif [ $DEV_TMP -ge $warning -a $DEV_TMP -lt $critical ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=WARNING
        echo "$state $msg"
        exit 1
elif [ $DEV_TMP -lt $warning -a $DEV_TMP -le $ok ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=OK
        echo "$state $msg"
        exit 0
else
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=UNKNOWN
        echo "$state $msg"
        exit 3
fi

Think I've figured it, the ok section needs to be an or?

druuna 08-12-2010 05:12 AM

Hi,

Why the complicated rules?

Code:

if [ $DEV_TMP -ge $critical ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=CRITICAL
        echo "$state $msg"
        exit 2
elif [ $DEV_TMP -ge $warning ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=WARNING
        echo "$state $msg"
        exit 1
elif [ $DEV_TMP -lt $warning ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=OK
        echo "$state $msg"
        exit 0


genderbender 08-12-2010 05:18 AM

Quote:

Originally Posted by druuna (Post 4063917)
Hi,

Why the complicated rules?

Code:

if [ $DEV_TMP -ge $critical ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=CRITICAL
        echo "$state $msg"
        exit 2
elif [ $DEV_TMP -ge $warning ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=WARNING
        echo "$state $msg"
        exit 1
elif [ $DEV_TMP -lt $warning ];then
        msg="temperature currently "$DEV_TMP_DEC"C."
        state=OK
        echo "$state $msg"
        exit 0


Heh... I have no idea why I overcomplicated that, thanks!

grail 08-12-2010 10:05 AM

Might be more obvious if you use the symbols you know by using the intended tools :)
Code:

if (( DEV_TMP >= critical ));then
    <blah>
elif (( DEV_TMP >= warning ));then
    <blah>
elif (( DEV_TMP < warning ));then

This appears clearer to the layman eye, IMHO

paulsm4 08-12-2010 11:44 AM

Hi -

I think the problem might be "floating point" vs "integer" comparisons.

Check out this article and see if it's applicable to your problem:

http://www.linuxjournal.com/content/...oint-math-bash

'Hope that helps .. PSM

grail 08-13-2010 01:38 AM

Quote:

I think the problem might be "floating point" vs "integer" comparisons.
He has removed the decimal to get around that.

genderbender 08-13-2010 03:38 AM

I'm pretty sure it was this bit:

Code:

$DEV_TMP -lt $warning -a $DEV_TMP -le $ok
The and in there makes those conditions impossible to meet:
lets say my warning level is 27.00 and my ok level is 26.00 and the current temperature is 26.50:

is 26.50 less than warning and less than ok... no its less than warning and more than ok. The initial post regarding keeping things simple was pretty spot on to be honest :). For some reason I only realise my mistakes once they're posted in forums, hehe.


All times are GMT -5. The time now is 10:49 AM.