LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2004, 12:01 PM   #1
bennethos
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: -- Slackware for servers -- Debian for desktops --
Posts: 124

Rep: Reputation: 16
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 !
 
Old 10-17-2004, 12:49 PM   #2
m00t00
Member
 
Registered: Sep 2004
Distribution: Slackware 10, Gentoo
Posts: 292

Rep: Reputation: 30
send the numbers to bc(1) to do the division. Make sure you set the precision though. (read the man page its good)
 
Old 10-17-2004, 12:58 PM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
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.
 
Old 10-17-2004, 01:18 PM   #4
bennethos
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: -- Slackware for servers -- Debian for desktops --
Posts: 124

Original Poster
Rep: Reputation: 16
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 , i tried to read the manpages, but without examples it's a little too hard for me

thx for helping me out that fast
 
Old 10-17-2004, 01:46 PM   #5
bennethos
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: -- Slackware for servers -- Debian for desktops --
Posts: 124

Original Poster
Rep: Reputation: 16
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!
 
Old 10-17-2004, 01:51 PM   #6
bennethos
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: -- Slackware for servers -- Debian for desktops --
Posts: 124

Original Poster
Rep: Reputation: 16
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
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Problem with Math and angles caged Programming 2 09-08-2005 01:12 AM
math division in javascript rblampain Programming 2 09-07-2005 04:07 AM
stuck again (noob bash math question) babag Programming 6 04-25-2005 01:27 AM
ip address class division emailssent Linux - Networking 3 10-08-2004 06:38 AM
Problem with math.h loke137 Programming 4 02-12-2004 07:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration