| | |
integer division! oh the fun :-)
Thread Solved
![]() |
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
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
Gives the output:
Whereas it should give:
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)
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:
Shell Scripting Syntax (Toggle Plain Text)
rBytes=694127 oBytes=769020 read=0 Mbps write=0 Mbps rBytes=17061 oBytes=141840 read=0 Mbps write=0 Mbps
Whereas it should give:
Shell Scripting Syntax (Toggle Plain Text)
rBytes=694127 oBytes=769020 read=0.69 Mbps write=0.76 Mbps rBytes=17061 oBytes=141840 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!
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
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)
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"
If my post helped add to my rep!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
Hey there,
One way to pass variables to awk (so you don't have to use double quotes) is with the -v flag, like
and then you could use the variable rBytes1 in your script just like any other awk variable.
Best wishes,
Mike
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...}'
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
bc(1) is your friend. It computes to arbitrary precision.
"-e" tells bash to interpret \x sequences; ksh might not need it.
"-e" tells bash to interpret \x sequences; ksh might not need it.
Shell Scripting Syntax (Toggle Plain Text)
. . . integer read=`echo -e "scale=2\n$rBytes/1000000" | bc` integer write=`echo -e "scale=2\n$wBytes/1000000" | bc` . . .
Last edited by Fest3er; Dec 12th, 2008 at 6:14 pm.
![]() |
Similar Threads
- Initialization Error (Java)
- The Calculator (C++)
Other Threads in the Shell Scripting Forum
- Previous Thread: Best way to skip first line?
- Next Thread: delete line + next line, if word match
| Thread Tools | Search this Thread |






