Can't resist a shell challenge, can I?... :)
On Tue, 18 Mar 2003, Jeff Waugh wrote:
> Here's some silly shell...
>
> $ grep bogomips /proc/cpuinfo | awk '{ print "+" $3 }' | tr -d '\n' | cut -c 2- |
> bc
> 6121.06
Very silly. awk + tr + cut is highly redundant and bc is not installed on
my system. :)
Here's something simpler that produces the same output for a single
processor without bc...:
$ grep bogomips /proc/cpuinfo | sed 's/[^0-9.]*//'
... although my system only says 729.08 :)
> Your mission: To work out what it's doing, why you'd be stupid enough to
> want to do it, and then how to do it better. It has to be in shell, and it
> has to handle decimals! :-)
Given that bc or dc are not guaranteed to be installed, I use expr
instead, but doing decimal calculations involves fixed-point maths.
all that given, the following works properly on a multi-cpu system:
$ expr 0 `sed -e 's/[0-9][0-9.]*/&000/' -e
's/^bogomips[^0-9.]*\([0-9]*\)\(.\([0-9][0-9][0-9]\)\)*.*/+ \1\3/' -e
'/^[^+]*$/d' /proc/cpuinfo` | sed 's/[0-9][0-9][0-9]$/.&/'
... be careful of the back-ticks and single quotes. I tend to use single
quotes to give my sed scripts better protection from the shell. the $ is
guaranteed not to be misinterpreted that way.
Ok, it's nasty, but it does it in 3 processes and with shell utilities
that will always be available.
Basically, Sed appends extra zeroes to all numbers, converts bogomips line
to a plus sign and a fixed three decimal places, and throws away the rest.
I then use expr to add the bogomips values to zero and put the decimal
point back in with a second sed.
now to why?:
To tell the user how long something's going to take, of course! :)
It's a slackware tradition to estimate how long something is going to take
in BogomipSeconds... the number never changes, but as you get more
Bogomips, you end up with less seconds. This chunk would give the shell
script a way of turning that abstract number into a time that an ordinary
user will understand. :)
Do I get the prize? :)
-- Jess
(Everything with a grin :)
--
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug