LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash and math division problem (https://www.linuxquestions.org/questions/programming-9/bash-and-math-division-problem-243797/)

bennethos 10-17-2004 12:01 PM

bash and math division problem
 
Hi all,

I did some scripting that counts total input and output traffic of eth0 from my iptables ...

I'm stuck with a nasty problem now.

# Name of the result file
STDIN=result

#Cat the file
CAT=$(cat $STDIN)

#RRD moet de waarden maar optellen met COUNTER
#Put the output one after the other separated with blank spaces and make a #white space a + sign
REPLACE=$(echo $CAT | sed -e s/\ /+/)

# Do some incrementing calc /1048576 for Mbytes /1024 for Kbytes
let z=$REPLACE
echo $z #<-- this gives a nice output of my input + my output

echo
echo Result in Kbytes
echo `expr $z / 1024` # here i'm dividing it to have kbytes value

echo
echo Result in Mbytes
echo `expr $z / 1048576`# same for mb



Cause now it works but he's modding the division eg . $z / 1048576 = 456,564214654 and he makes 456 of it if you get my point.... that's a loss in monitorring of half a megabyte !

This causes a massive difference after 30 days of monitorring the traffic !


Can anyone help me out please !

thx a lot !

m00t00 10-17-2004 12:49 PM

send the numbers to bc(1) to do the division. Make sure you set the precision though. (read the man page its good)

rjlee 10-17-2004 12:58 PM

You can use expr to return the remainder of division (i.e. the number of bytes left over) using % instead of /
Code:

echo Result in Kbytes
echo -n `expr $z / 1024`
echo -n " kb and "
echo -n `expr $z % 1024`
echo " bytes"

Alternativly, try using a calculator tool like bc instead of expr.

bennethos 10-17-2004 01:18 PM

thx a lot

I decided to use bc, cause i need to update those values in my Round Robin database ...


did some searching and found following simple example :

bc cal.txt = Would result in 3, see below notes.

Within the cal.txt file you could have a simple statement such as:

/* Add the value 1+2 /*
1+2
quit

When running the above command you will receive the results of the cal.txt file. Which in this case would be 3.


well i made a file gave it 777 permission to be sure and when i do bc cal.txt = i get :

bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
File = is unavailable.

does anyone have a clue :p, i tried to read the manpages, but without examples it's a little too hard for me

thx for helping me out that fast

bennethos 10-17-2004 01:46 PM

found it allreade :

i = 7 / 3
i
quit

when you runt that script using bc -l u will get floating point value in return :)

just wondering how to simplify to JUST 3 numbers after the , or . cause now i get 2.33333333333



th x!

bennethos 10-17-2004 01:51 PM

hmmz found it as well :)

u have to put scale = 3 in top of ur script :)

this program rulez !!!

thx a lot guys for putting me on the right track


All times are GMT -5. The time now is 05:44 AM.