Work the Shell - Breaking Numbers Down
Last month, we continued our journey into the dark caverns of Apache Web logs, examining how relatively simple shell scripts can be utilized to produce useful and important data. The specific script we created searched a log file for traffic that occurred the previous day, summarizing the number of bytes transmitted.
That's all well and good, but as with many shell scripts, there's a bit of a problem with this one, which was immediately obvious when my busy site produced an estimated monthly data transfer rate of 2346990660 bytes.
Clearly that's a very human-unfriendly number, and doubly so without any commas to break it up into thousands, millions and so on. More important, when talking about data transfer, we're used to thinking in terms of powers of two, so 1 kilobyte is 1024 bytes of data, not 1000 bytes of data, and 1 megabyte is 1024 kilobytes of data, and so on.
Unfortunately, the expr command that we're using for the mathematical calculations doesn't have the ability to work with these powers of two, so we're going to have to do the work ourselves, converting massive numbers into more readable KB, MB or GB values, as appropriate.
The basics are pretty easy:
kilo="$(( $value / 1024 ))" mega="$(( $kilo / 1024 ))" giga="$(( $mega / 1024 ))"
Given a nice huge number like 2346990660, the results are then quickly calculated:
$ sh -x convert.sh 2346990660 + value=2346990660 + kilo=2291983 + mega=2238 + giga=2 + exit 0
(Helpful tip: the -x option lets you debug shell scripts by showing, line by line, what command is being executed.)
The problem with this approach is immediately obvious when we switch from a huge number, more than 2GB, to a smaller value:
$ sh -x convert.sh 5000 + value=5000 + kilo=4 + mega=0 + giga=0 + exit 0
We don't want zero values; we want to see the fractional decimal values, which means not only that we can't use the built-in mathematical capabilities of the shell, but we also can't use expr. Instead, we need to move into the crufty, ancient world of bc, the binary calculator.
Now, bc isn't for the faint of heart, but to save you from reading the man page, here's how you can force four digits after the decimal point on the result of a division that results in a value less than 1.0:
$ echo "scale=2 ; 3000 / 30001" | bc .0999
Can you see how to put these together? Here's a new, far-improved way to calculate kilo, mega and giga:
$ sh -x convert.sh 5000 + value=5000 ++ echo 'scale=2; 5000 / 1024' ++ bc + kilo=4.88 ++ echo 'scale=2; 4.88 / 1024' ++ bc + mega=0 ++ echo 'scale=2; .00 / 1024' ++ bc + giga=0 + exit 0
The debug output from the -x option is getting a bit confusing here, I admit, but you now can see that kilo is set to 4.88 when given the initial value of 5000 bytes, and that both mega and giga are zero.
Let's try again (and I'll clean up some of the spurious debug output from this point on, for clarity) with the initial really big value:
$ convert.sh 2346990660 value=2346990660 kilo=2291983.06 mega=2238.26 giga=2.18
Cool. Now we can finally see that we're talking about 2.18GB of data being transferred off the site each month—far more coherent than the huge value shown earlier.
Now, let's figure out how to show always the most logical of these values, rather than all of them.
The easiest way to figure out which value is best is simply to ascertain where the value drops below 1.0. In the case of 5000 bytes, that'd be best displayed as 4.88KB, and in the case of the bignum value, that's 2.18GB.
To figure out when the value drops below zero, we'd love to have a floating-point numeric comparison, but sadly, the shell can't manage it. If you try it, you'll just get the error “integer expression expected”.
There are a number of ways to get the “floor” of the value, but I use bc again here to do the job by calculating the division once more, this time without any scale value at all:
kiloint=$( echo "$value/1024" | bc)"
Doing this gets just the integer portion of the $kilo value, and that can indeed be tested in a conditional statement:
if [ $kiloint -lt 1 ] ; then
Now, put it all together, and here's how the script looks:
kilo=$( echo "scale=2; $value / 1024" | bc )
kiloint=$( echo "$value / 1024" | bc )
mega=$( echo "scale=2; $kilo / 1024" | bc )
megaint=$( echo "$kilo / 1024" | bc )
giga=$( echo "scale=2; $mega / 1024" | bc )
gigaint=$( echo "$mega / 1024" | bc )
if [ $kiloint -lt 1 ] ; then
echo "$value bytes"
elif [ $megaint -lt 1 ] ; then
echo "${kilo}KB"
elif [ $gigaint -lt 1 ] ; then
echo "${mega}MB"
else
echo "${giga}GB"
fi
A little funky, but it certainly works exactly as we'd hope:
$ sh convert.sh 5000000000 4.65GB $ sh convert.sh 5000000 4.76MB $ sh convert.sh 50000 48.82KB $ sh convert.sh 50 50 bytes
The final step is to make it a function so we can include it in other shell scripts and access it as desired. This is done within the Bourne Shell by giving it a unique name and then wrapping the functional code in braces:
kmg()
{
code for function goes here, params are $1, $2, etc.
}
This can then be invoked within a shell script by name (k=kilo, m=mega, g=giga):
kmg 500000
More important, you can embed it within a line by using a subshell notation, so given the kmg() function, the following two-line script works splendidly:
echo given value is $1 echo which converts to $(kmg $1)
That's nice and short, and if the kmg function is dropped into its own file, you also can use the . command to include another file in the shell script, meaning that the entire test script is now:
#!/bin/sh . kmg.sh echo The given value $1 bytes = $(kmg $1) exit 0
I'm out of space here, but I hope you can see how this approach can be applied to a wide variety of different shell tasks, making your shell scripts far more efficient and faster to write too!
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- RSS Feeds
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Reply to comment | Linux Journal
16 min 38 sec ago - Android has been dominating
21 min 10 sec ago - It is quiet helping
3 hours 6 min ago - Technology
3 hours 23 min ago - Reachli - Amplifying your
4 hours 40 min ago - excellent
5 hours 29 min ago - good point!
5 hours 32 min ago - Varnish works!
5 hours 41 min ago - Reply to comment | Linux Journal
6 hours 10 min ago - Reply to comment | Linux Journal
8 hours 36 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Function for bigger numbers
Here is a little Korn Shell implementation based on your article that supports up to Yotta Bytes Yikes!
I thought I'd share it with your readers.
#!/bin/ksh bytestohr() { # Convert input parameter (number of bytes) # to Human Readable form # SLIST="bytes,KB,MB,GB,TB,PB,EB,ZB,YB" POWER=1 VAL=$( echo "scale=2; $1 / 1" | bc) VINT=$( echo $VAL / 1024 | bc ) while [ $VINT -gt 0 ] do let POWER=POWER+1 VAL=$( echo "scale=2; $VAL / 1024" | bc) VINT=$( echo $VAL / 1024 | bc ) done echo $VAL$( echo $SLIST | cut -f$POWER -d, ) }NOTE: It's tempting to use the shell internal maths instead of bc for more performance, but values over 8Gb aren't supported by the shell's 32bit integers.
Oops mistake above
As soon as you make a post you find a mistake, the above while statement should be changed to
To avoid numeric overflows in some Korn shell implementations.