DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   integer division! oh the fun :-) (http://www.daniweb.com/forums/thread161164.html)

chris5126 Dec 7th, 2008 11:55 am
integer division! oh the fun :-)
 
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
 integer rBytes=`kstat -p -c net -n $Interface -s rbytes64 1 2 |\
                tail -1 | awk '{print $2}'`
  integer oBytes=`kstat -p -c net -n $Interface -s obytes64 1 2 |\
                tail -1 | awk '{print $2}'`
  integer read=$(($rBytes/1000000))
  integer write=$(($oBytes/1000000))
  k=k+1
  echo "rBytes=$rBytes oBytes=$oBytes"
  echo "read=$read Mbps write=$write Mbps"

Gives the output:
rBytes=694127 oBytes=769020
read=0 Mbps write=0 Mbps
rBytes=17061 oBytes=141840
read=0 Mbps write=0 Mbps

Whereas it should give:
rBytes=694127 oBytes=769020
read=0.69 Mbps write=0.76 Mbps
rBytes=17061 oBytes=141840
read=0.17 Mbps write=0.14 Mbps

Salem Dec 7th, 2008 12:16 pm
Re: integer division! oh the fun :-)
 
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.

chris5126 Dec 7th, 2008 1:12 pm
Re: integer division! oh the fun :-)
 
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.

 Interface=`cat $Interfaces | sed -n $k"p"`
  integer rBytes=`kstat -p -c net -n $Interface -s rbytes64 1 2 |\
                tail -1 | awk '{print $2}'`
  awk '{print $rBytes / 100000}'END
  integer oBytes=`kstat -p -c net -n $Interface -s obytes64 1 2 |\
                tail -1 | awk '{print $2}'`
  #integer read=$(($rBytes/1000000))
  #integer write=$(($oBytes/1000000))
  k=k+1
  echo "rBytes=$rBytes oBytes=$oBytes"

Salem Dec 7th, 2008 1:51 pm
Re: integer division! oh the fun :-)
 
> awk '{print $rBytes / 100000}'END
How is that anything like what I posted?

chris5126 Dec 7th, 2008 2:13 pm
Re: integer division! oh the fun :-)
 
haha i was trying to put the value into a varailble and was getting frusturtated and trying different things! thanks for your help!

eggi Dec 8th, 2008 12:46 am
Re: integer division! oh the fun :-)
 
Hey there,

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

Quote:

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

Fest3er Dec 12th, 2008 6:13 pm
Re: integer division! oh the fun :-)
 
bc(1) is your friend. It computes to arbitrary precision.

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

  .
  .
  .
  integer read=`echo -e "scale=2\n$rBytes/1000000" | bc`
  integer write=`echo -e "scale=2\n$wBytes/1000000" | bc`
  .
  .
  .


All times are GMT -4. The time now is 6:37 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC