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

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.

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"

> awk '{print $rBytes / 100000}'END
How is that anything like what I posted?

haha i was trying to put the value into a varailble and was getting frusturtated and trying different things! thanks for your help!

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

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`
  .
  .
  .
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.