integer division! oh the fun :-)

Thread Solved

Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

integer division! oh the fun :-)

 
0
  #1
Dec 7th, 2008
Hi guys,

Will keep this short and sweet. Trying to do integer devision to *decimial places but i dont think the version of ksh installed on solaris 10 supports this as they would need to be declared as floats. Is this correct?

If there isnt a way of doing it with ksh is there another way to do it
Shell Scripting Syntax (Toggle Plain Text)
  1. integer rBytes=`kstat -p -c net -n $Interface -s rbytes64 1 2 |\
  2. tail -1 | awk '{print $2}'`
  3. integer oBytes=`kstat -p -c net -n $Interface -s obytes64 1 2 |\
  4. tail -1 | awk '{print $2}'`
  5. integer read=$(($rBytes/1000000))
  6. integer write=$(($oBytes/1000000))
  7. k=k+1
  8. echo "rBytes=$rBytes oBytes=$oBytes"
  9. echo "read=$read Mbps write=$write Mbps"

Gives the output:
Shell Scripting Syntax (Toggle Plain Text)
  1. rBytes=694127 oBytes=769020
  2. read=0 Mbps write=0 Mbps
  3. rBytes=17061 oBytes=141840
  4. read=0 Mbps write=0 Mbps

Whereas it should give:
Shell Scripting Syntax (Toggle Plain Text)
  1. rBytes=694127 oBytes=769020
  2. read=0.69 Mbps write=0.76 Mbps
  3. rBytes=17061 oBytes=141840
  4. read=0.17 Mbps write=0.14 Mbps
Last edited by chris5126; Dec 7th, 2008 at 11:59 am.
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: integer division! oh the fun :-)

 
0
  #2
Dec 7th, 2008
Personally, I would just do
awk '{print $2 / 1000000 }'
and stop worrying about just how good the maths is in the shell.

Also, awk has printf() as well, so you've got really good control over the format as well.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: integer division! oh the fun :-)

 
0
  #3
Dec 7th, 2008
ok not to familar with awk had a little play but getting errors. So if i wanted to manipluate the varialbes and then put them into the new varialbles read and write how would i do it. Also would be good if you could show me how to do it to two decimal places.

Shell Scripting Syntax (Toggle Plain Text)
  1. Interface=`cat $Interfaces | sed -n $k"p"`
  2. integer rBytes=`kstat -p -c net -n $Interface -s rbytes64 1 2 |\
  3. tail -1 | awk '{print $2}'`
  4. awk '{print $rBytes / 100000}'END
  5. integer oBytes=`kstat -p -c net -n $Interface -s obytes64 1 2 |\
  6. tail -1 | awk '{print $2}'`
  7. #integer read=$(($rBytes/1000000))
  8. #integer write=$(($oBytes/1000000))
  9. k=k+1
  10. echo "rBytes=$rBytes oBytes=$oBytes"
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: integer division! oh the fun :-)

 
0
  #4
Dec 7th, 2008
> awk '{print $rBytes / 100000}'END
How is that anything like what I posted?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: integer division! oh the fun :-)

 
0
  #5
Dec 7th, 2008
haha i was trying to put the value into a varailble and was getting frusturtated and trying different things! thanks for your help!
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: integer division! oh the fun :-)

 
0
  #6
Dec 8th, 2008
Hey there,

One way to pass variables to awk (so you don't have to use double quotes) is with the -v flag, like

awk -v rBytes1=$rBytes '{......blah,blah,blah...}'
and then you could use the variable rBytes1 in your script just like any other awk variable.

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: integer division! oh the fun :-)

 
0
  #7
Dec 12th, 2008
bc(1) is your friend. It computes to arbitrary precision.

"-e" tells bash to interpret \x sequences; ksh might not need it.

Shell Scripting Syntax (Toggle Plain Text)
  1. .
  2. .
  3. .
  4. integer read=`echo -e "scale=2\n$rBytes/1000000" | bc`
  5. integer write=`echo -e "scale=2\n$wBytes/1000000" | bc`
  6. .
  7. .
  8. .
Last edited by Fest3er; Dec 12th, 2008 at 6:14 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC