LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2010, 04:51 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
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?

Last edited by genderbender; 08-12-2010 at 04:55 AM.
 
Old 08-12-2010, 05:12 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
1 members found this post helpful.
Old 08-12-2010, 05:18 AM   #3
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by druuna View Post
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!
 
Old 08-12-2010, 10:05 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
1 members found this post helpful.
Old 08-12-2010, 11:44 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 08-13-2010, 01:38 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
I think the problem might be "floating point" vs "integer" comparisons.
He has removed the decimal to get around that.
 
Old 08-13-2010, 03:38 AM   #7
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.

Last edited by genderbender; 08-13-2010 at 03:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
if statement malikah Programming 2 07-10-2008 10:06 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
For Statement bendeco13 Linux - General 6 10-31-2004 05:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration