comparing float with int / number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing float with int / number
# 1  
Old 05-19-2005
Question comparing float with int / number

Hi all,
I'm looking to modify a script to check disk space usage.

Here is the code at the moment:

Code:
#
# The control file, MONITOR_DISK_SPACE, must be in the format ... Drive:;threshold_percentage
# eg.
# C:;95
# D:;98
# E:;90
#
# For each line in the control file (MONITOR_DISK_SPACE) determine the drive to be checked.
#
for i in `cat $MONITOR_DISK_SPACE`
do
  MONITOR_DISK_DRIVE=`echo $i | cut -f1 -d';'`
  DISK_SPACE_THRESHOLD=`echo $i | cut -f2 -d';'`
  DISK_SPACE_USED=`df -Pk | grep -i $MONITOR_DISK_DRIVE | awk '{print $3}'`
  DISK_SPACE_TOTAL=`df -Pk | grep -i $MONITOR_DISK_DRIVE | awk '{print $2}'`
  DISK_SPACE_PERCENTAGE=`echo "scale=2; $DISK_SPACE_USED / $DISK_SPACE_TOTAL * 100" | bc` 
  #
  # Check that the drive is not 100% full.  If so report the error.
  #
  if [ $DISK_SPACE_PERCENTAGE -eq 100 ]  then
      echo "CRITICAL ERROR: Disk drive $MONITOR_DISK_DRIVE is 100% full" >> $LOG_FILE
      SEND_MAIL="Y"
  fi
  #
  # Check that the drive has not used space greater than the threshold.  If so report as a warning
  #
  if [ $DISK_SPACE_PERCENTAGE -gt $DISK_SPACE_THRESHOLD ]
  then
      echo "WARNING: Drive $MONITOR_DISK_DRIVE is at $DISK_SPACE_USED% capacity. This is greater than the check threshold of $DISK_SPACE_THRESHOLD%" >> $LOG_FILE
      SEND_MAIL="Y"
  fi
done
#

This is an extract from a larger script.

The line highlighted in red is obviously not going to work because it is comparing a float with an integer/number. How can I modify this if statement so that it can compare $DISK_SPACE_PERCENTAGE with 100 and the same with the second IF comparing the percentage used with a threshold amount?

Also you might ask why am I trying to calculate the percentage free when df already gives me this - well I am using MKSTOOL kit on a windows platform and there appears to be a bug with df reporting incorrect capacity percentages. See below:

Code:
$ df -Pk
Filesystem         1024-blocks        Used  Available  Capacity Mounted on
C:/                   12586927     4154963    8431964       34% C:/
D:/                   22916688    16775468    6141220       74% D:/
E:/                   71087592    61504460    9583132       27% E:/
//ehrperth1/e$        40909520    26162176   14747344       64% H:/

Any help or pointer on this?

Cheers,
Leigh.

Last edited by lelliott; 05-19-2005 at 06:47 AM..
# 2  
Old 05-19-2005
# 3  
Old 05-19-2005
Thanks for this.

Cheers,
Leigh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python : Problem with " TypeError: float() argument must be a string or a number "

Hello ! I'm creating a CGI which allow to display graph from some data. The datas looks like : 2020-01-13-00-00,384.00,350.00 2020-01-13-06-00,384.00,350.00 2020-01-13-12-00,384.00,350.00 2020-01-13-18-00,384.00,350.00 2020-01-14-00-00,384.00,350.00... (1 Reply)
Discussion started by: Tim2424
1 Replies

2. Shell Programming and Scripting

[awk] rounding a float number?

Heyas Trying to calculate the total size of a file by reading its bitrate. Code snippet: fs_expected() { # # Returns the expected filesize in bytes # pr_str() { ff=$(cat $TMP.info) d="${ff#*bitrate: }" echo "${d%%,*}" | $AWK '{print $1}' | head -n 1 } t_BYTERATE=$((... (9 Replies)
Discussion started by: sea
9 Replies

3. UNIX for Dummies Questions & Answers

Comparing a number in a text file with a specific value

My project is to get a temperature reading from a refridgerator every 2 minutes and check to see if the door has been left open. I don't yet have the mastery of Linux, being a complete noob, but I reckon I need a text file with the latest temperature reading in it. This I've managed to do by... (2 Replies)
Discussion started by: Fitch
2 Replies

4. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

5. Shell Programming and Scripting

Float number format problem -Awk

Here is the script I'm using awk '{print $1,"\t",(($2+$3)/2)-x,"\t",(($2+$3)/2)+x,"\t",$4,"\t",$5}' x=500 $1 I just want to make float numbers (red) like normal numbers (green) output cX 1.65107e+08 1.65108e+08 13 64.2 cX 165112764 165113764 27 ... (7 Replies)
Discussion started by: ruby_sgp
7 Replies

6. Programming

Adding 2 difft int to form a number

got 1 problem.. can someone help me wit the logic? Money Money ::operator+(const Money &rhs)const { Money temp; temp.a = a+rhs.a; temp.b = b+rhs.b; return temp; }//end i got 2 number e.g 6.2 and 3.8 (1 Reply)
Discussion started by: xiaojesus
1 Replies

7. Shell Programming and Scripting

Comparing String with Number variables

I have two variables and want to perform some functions after comparison $cat file1 14.abcde a=`cut -f 1 -d "." file1 b=15 if then .... fi but i got an error message says that unary operator expected and i think its because of $a is a string and trying to compare with... (3 Replies)
Discussion started by: bonosungho
3 Replies

8. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

9. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

10. Shell Programming and Scripting

comparing two float values

I am trying to compare 2 float values using if the foll code does not work a=1.4 b=1.6 if test $a -gt $b then echo "$a is max" else echo "$b is max" fi does -gt work for floating point numbers, if not how do go about for my requirement? can i use bc ? pls help thanks in advance... (2 Replies)
Discussion started by: kavitha
2 Replies
Login or Register to Ask a Question