![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 ]] |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|