Stupid issue with number comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stupid issue with number comparison
# 1  
Old 06-10-2009
Stupid issue with number comparison

Hello all,

I'm having an infuriating issue with number comparison. Basically I've written a script that runs in cygwin that SSH's to 4 servers, figures out a success percentage and if it is less than a certain point, triggers an alarm. I've managed to get it to connect to the servers, figure out the percentage and create the if statements. Unfortunately what should be the easiest part, comparing the numbers is driving me crazy.

Here is the script (i've added the part about printing the variables to help debug):

Code:
(22:42:27\[DeCo@deco)
[~]$ cat bkcheck.orig 
#!/usr/bin/bash
bkc ()
{
ssh dncsb tail -1 /dvs/dncs/tmp/PerformanceMonitoring/dsmperfmon.csv|awk -F, '{print ($3 / $4) * 100}'
}
qnc ()
{
ssh dncsq tail -1 /dvs/dncs/tmp/PerformanceMonitoring/dsmperfmon.csv|awk -F, '{print ($3 / $4) * 100}'
}
smc ()
{
ssh dncss tail -1 /dvs/dncs/tmp/PerformanceMonitoring/dsmperfmon.csv|awk -F, '{print ($3 / $4) * 100}'
}
nmc ()
{
ssh dncsn tail -1 /dvs/dncs/tmp/PerformanceMonitoring/dsmperfmon.csv|awk -F, '{print ($3 / $4) * 100}'
}
bkerrors=`bkc`
qnerrors=`qnc`
smerrors=`smc`
nmerrors=`nmc`
echo $bkerrors
echo $qnerrors
echo $smerrors
echo $nmerrors
while true;do
if 
[[ "$bkerrors" < "97" ]];then
 cygstart bk.wav
printf "Uh-oh, at `date +%H:%M:%S` Brooklyn VOD is streaming at $bkerrors%%\n"
sleep 5
else 
printf "As of `date +%H:%M:%S`,Brooklyn VOD is streaming at $bkerrors%%\n"
fi
if
[[ "$qnerrors" < "97" ]];then
cygstart qn.wav
printf "Uh-oh, at `date +%H:%M:%S` Queens VOD is streaming at $qnerrors%%\n"
sleep 5
else
printf "As of `date +%H:%M:%S`,Queens VOD is streaming at $qnerrors%%\n"
fi
if
[[ "$smerrors" < "97" ]];then
 cygstart sm.wav
printf "Uh-oh, at `date +%H:%M:%S` S.Manhattan VOD is streaming at $smerrors%%\n"
sleep 5
else
printf "As of `date +%H:%M:%S`,S.Manhattan VOD is streaming at $smerrors%%\n"
fi
if
[[ "$nmerrors" < "97" ]];then
 cygstart nm.wav
printf "Uh-oh, at `date +%H:%M:%S` N.Manhattan VOD is streaming at $nmerrors%%\n"
sleep 5
else
printf "As of `date +%H:%M:%S`,N.Manhattan VOD is streaming at $nmerrors%%\n"
fi

sleep 300
clear
bkerrors=`bkc`
qnerrors=`qnc`
smerrors=`smc`
nmerrors=`nmc`
done

[~]$ ./bkcheck.orig 
99.9454
100
99.8703
99.3868
As of 22:45:23,Brooklyn VOD is streaming at 99.9454%
Uh-oh, at 22:45:24 Queens VOD is streaming at 100%
As of 22:45:30,S.Manhattan VOD is streaming at 99.8703%
As of 22:45:30,N.Manhattan VOD is streaming at 99.3868%

The script runs without throwing an errors, but the logic doesn't work. If the current number is below 97 it does set off the alarm, but it also sets off the alarm if the percentage is 100 or higher. At first I thought it was something to do with the script only understanding integers, but that doesn't seem to be the case. I've tried every combination of single ([) brackets and double ([[) brackets I can think of and still getting the same thing. I've also tried quoting and not quoting the variables, but still the same errors.

I know that this is going to be something stupid...but I'm pulling my proverbial hair out here...
# 2  
Old 06-11-2009
Not sure whether you can use < for number comparision
try -lt i.e.
Code:
if [[ $num1 -lt 97 ]]
then
  do something;
fi

# 3  
Old 06-11-2009
Thanks for the try. I tried "-lt" as well and it didn't work. I don't have the output in front of me right now, but it would throw and actual error instead of just triggering the wrong response.

edit:

I get the following when I use -lt (and no quotes around anything)

[~]$ ./bkcheck.orig
99.9219
99.7949
99.8012
99.8723
./bkcheck.orig: line 28: [[: 99.9219: syntax error: invalid arithmetic operator (error token is ".9219")
As of 23:43:48,Brooklyn VOD is streaming at 99.9219%
./bkcheck.orig: line 36: [[: 99.7949: syntax error: invalid arithmetic operator (error token is ".7949")
As of 23:43:49,Queens VOD is streaming at 99.7949%
./bkcheck.orig: line 44: [[: 99.8012: syntax error: invalid arithmetic operator (error token is ".8012")
As of 23:43:49,S.Manhattan VOD is streaming at 99.8012%
./bkcheck.orig: line 52: [[: 99.8723: syntax error: invalid arithmetic operator (error token is ".8723")
As of 23:43:49,N.Manhattan VOD is streaming at 99.8723%

Last edited by DeCoTwc; 06-11-2009 at 12:52 AM..
# 4  
Old 06-11-2009
let me give it one more try
you are using bash shell. try this...
Code:
#!/bin/bash
num=1
if [ $num -lt 97 ];
then
echo do something
fi

it is working for me.
# 5  
Old 06-11-2009
That works but not if there are decimal places it fails. and I still don't understand why there were times when the var would be 100 and it would still fail.

I understand that the issue is it only works with integers, but there must be a way to compare non integers...or worst case round off to the closest whole number.
# 6  
Old 06-11-2009
please share modifed program with input and desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Linux

Linking issue due to so version number

Hi all, currently I'm facing a issue in linking a .so file. In my build machine, I've libcrypto.so.6 and there is a softlink as libcrypto.so. In my make file I'm trying to link to the lib using -L -lcrypto and it is success and created my test.exe. When I copy this test.exe to other... (4 Replies)
Discussion started by: vijkrr
4 Replies

3. Shell Programming and Scripting

Negative number comparison using nawk on Linux

Hi All, I am trying to compare two negative numbers using awk on linux.But it is giving me wrong result.Same code is working perfectly on solaris. print ((0+new_price) < MIN_PRICE) e.g If I try to compare -1.32(new_price) and -500(min_price) using "<" operator, output is 1 i.e true. ... (5 Replies)
Discussion started by: Rashmee
5 Replies

4. Shell Programming and Scripting

Number comparison in ksh on mac with -lt is giving wrong answer

I am trying to run following script in ksh on darwin 11.4.2: freeSpace=2469606195 spaceNeeded=200 ] && echo "no space" || echo "space available" ] && echo "no space" || echo "space available" "-lt" is giving wrong answer as "no space" Whereas '<' works fine. When I change the freespace... (4 Replies)
Discussion started by: sabitha
4 Replies

5. Shell Programming and Scripting

Issue with files comparison, help me with a logic

Can someone please help me with a unix logic for below. I tried to get the desired output by using change capture condition in Datastage but its not working properly. i have two files file1, file2 as below. file1 ROW_NO VEND_NO CODE AIR_D OCEAN_D ---------------------------------------- 1 ... (3 Replies)
Discussion started by: JSKOBS
3 Replies

6. Shell Programming and Scripting

comparison of number in linux..

hi all experts, i=1; while do echo $i $i=$i+1 done can I use min=2 max=5 if (($min > $ max)) then else (2 Replies)
Discussion started by: hamnsan
2 Replies

7. Shell Programming and Scripting

Issue with String Comparison (if)

Hi, I was trying to do a string comparison using if. However, the comparison result is getting treated as a executable statement. I'm not sure where I'm making the mistake! $ typeset TEST_VAR='YUP' $ if ; then echo 'Got It!'; fi; ksh: : not found. Any help is appreciated! (3 Replies)
Discussion started by: waterdrop
3 Replies

8. UNIX for Advanced & Expert Users

Strange Number comparison issue

Hi, I am comparing two numbers, but it gives strange results: My Code: if then echo "True" else echo "False" fi This code gives False for the follwoing comparison where as True for the following: Any reason for this? Both Should have given False... I am using... (9 Replies)
Discussion started by: shihabvk
9 Replies

9. Programming

random number logic -- issue

I use standard C random number generation logic in my application. long nCounter; long lRndNo; char rand; srand48(nCounter); lRndNo = lrand48(); sprintf(rand,"%010.10d",lRndNo); However we always find that the rand generated starts with '0','1' or '2'. I could not logically prove if... (1 Reply)
Discussion started by: asutoshch
1 Replies
Login or Register to Ask a Question