The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Is grep being used correctly? sai0899 Shell Programming and Scripting 2 04-18-2008 12:03 AM
Shell Implementation not working correctly AirBronto High Level Programming 14 02-15-2008 10:41 PM
Variable not working correctly. walsh_j Shell Programming and Scripting 3 05-30-2007 02:50 PM
Script not working correctly elchalateco UNIX for Dummies Questions & Answers 2 10-11-2002 04:09 PM
why the PATH can not be set correctly? yishen UNIX for Dummies Questions & Answers 5 07-23-2002 10:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-01-2007
2dumb 2dumb is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 40
if not working correctly

Anyone have an idea why this if statement does not work correctly?

"test2.sh" 18 lines, 386 characters
#!/usr/bin/sh

WARNING=80
CRITICAL=95

check_it()
{
if [[ ${1} = ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]];then
echo "YES [[ ${1} = ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]]"
else
echo "NO [[ ${1} = ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]]"
fi
}

check_it 80.1
check_it 81.1

me> test2.sh
NO [[ 80.1 = 80 ]] || [[ 80.1 -gt 80 && 80.1 -lt 95 ]]
YES [[ 81.1 = 80 ]] || [[ 81.1 -gt 80 && 81.1 -lt 95 ]]
  #2 (permalink)  
Old 05-01-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
The following works.
Compare this version with your version and see what the problems were.
Code:
#!/usr/bin/sh

WARNING=80
CRITICAL=95

check_it()
{
if [ ${1} -eq ${WARNING} -o \( ${1} -gt ${WARNING} -a ${1} -lt ${CRITICAL} \) ];
then
echo "YES [[ ${1} -eq ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRIT
ICAL} ]]"
else
echo "NO [[ ${1} -eq ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITI
CAL} ]]"
fi
}

check_it 79.7
check_it 80.0
check_it 80.1
check_it 81.1

Last edited by Shell_Life; 05-01-2007 at 02:10 PM..
  #3 (permalink)  
Old 05-01-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by 2dumb
Anyone have an idea why this if statement does not work correctly?

The standard shell cannot do floating point comparisons or arithmetic. You need to use ksh93.

  #4 (permalink)  
Old 05-03-2007
2dumb 2dumb is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 40
Thanks Shell_Life,
It seems all I needed to do is change my test from "=" to "-eq".

cfajohnson,
You are also correct if I try to compare to 80.1 instead of a whole number then it returns untrue results (see below). However I will only use whole numbers so I should be ok.

me> cat test2.sh
#!/usr/bin/sh

WARNING=80
CRITICAL=95

check_it_before()
{
if [[ ${1} = ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]];then
echo "TRUE [[ ${1} = ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]]"
else
echo "FALSE [[ ${1} = ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]]"
fi
}

check_it_after()
{
if [[ ${1} -eq ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]];then
echo "TRUE [[ ${1} -eq ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]]"
else
echo "FALSE [[ ${1} -eq ${WARNING} ]] || [[ ${1} -gt ${WARNING} && ${1} -lt ${CRITICAL} ]]"
fi
}

echo Before
check_it_before 79.9
check_it_before 80.0
check_it_before 80.1
check_it_before 94.4
check_it_before 95.0
check_it_before 95.1

echo "\nAfter"
check_it_after 79.9
check_it_after 80.0
check_it_after 80.1
check_it_after 94.9
check_it_after 95.0
check_it_after 95.1

me> test2.sh
Before
FALSE [[ 79.9 = 80 ]] || [[ 79.9 -gt 80 && 79.9 -lt 95 ]]
FALSE [[ 80.0 = 80 ]] || [[ 80.0 -gt 80 && 80.0 -lt 95 ]]
FALSE [[ 80.1 = 80 ]] || [[ 80.1 -gt 80 && 80.1 -lt 95 ]]

TRUE [[ 94.4 = 80 ]] || [[ 94.4 -gt 80 && 94.4 -lt 95 ]]
FALSE [[ 95.0 = 80 ]] || [[ 95.0 -gt 80 && 95.0 -lt 95 ]]
FALSE [[ 95.1 = 80 ]] || [[ 95.1 -gt 80 && 95.1 -lt 95 ]]


After
FALSE [[ 79.9 -eq 80 ]] || [[ 79.9 -gt 80 && 79.9 -lt 95 ]]
TRUE [[ 80.0 -eq 80 ]] || [[ 80.0 -gt 80 && 80.0 -lt 95 ]]
TRUE [[ 80.1 -eq 80 ]] || [[ 80.1 -gt 80 && 80.1 -lt 95 ]]
TRUE [[ 94.9 -eq 80 ]] || [[ 94.9 -gt 80 && 94.9 -lt 95 ]]
FALSE [[ 95.0 -eq 80 ]] || [[ 95.0 -gt 80 && 95.0 -lt 95 ]]
FALSE [[ 95.1 -eq 80 ]] || [[ 95.1 -gt 80 && 95.1 -lt 95 ]]


Note when I try to compare to 80.1 instead of a whole number then it returns untrue results.
After
FALSE [[ 79.9 -eq 80.1 ]] || [[ 79.9 -gt 80.1 && 79.9 -lt 95 ]]
TRUE [[ 80.0 -eq 80.1 ]] || [[ 80.0 -gt 80.1 && 80.0 -lt 95 ]]
TRUE [[ 80.1 -eq 80.1 ]] || [[ 80.1 -gt 80.1 && 80.1 -lt 95 ]]
TRUE [[ 94.9 -eq 80.1 ]] || [[ 94.9 -gt 80.1 && 94.9 -lt 95 ]]
FALSE [[ 95.0 -eq 80.1 ]] || [[ 95.0 -gt 80.1 && 95.0 -lt 95 ]]
FALSE [[ 95.1 -eq 80.1 ]] || [[ 95.1 -gt 80.1 && 95.1 -lt 95 ]]
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0