42 Improving the Readability of df Output

#42 Improving the Readability of df Output

While Script #41 summarized df command output, the most important change we can make to df is simply to improve the readability of its output.

The Code

 #!/bin/sh # newdf - A friendlier version of df. awkscript="/tmp/newdf.$$" trap "rm -f $awkscript" EXIT cat << 'EOF' > $awkscript function showunit(size) { mb = size / 1024; prettymb=(int(mb * 100)) / 100;   gb = mb / 1024; prettygb=(int(gb * 100)) / 100;   if (substr(size,1,1) !~ "[0-9]"         substr(size,2,1) !~ "[0-9]") { return size }   else if (mb < 1) { return size "K" }   else if (gb < 1) { return prettymb "M" }   else             { return prettygb "G" } } BEGIN {   printf "%-27s %7s %7s %7s %8s %-s\n",         "Filesystem", "Size", "Used", "Avail", "Capacity", "Mounted" } !/Filesystem/ {   size=showunit();   used=showunit();   avail=showunit();   printf "%-27s %7s %7s %7s %8s %-s\n",         , size, used, avail, ,  } EOF df -k  awk -f $awkscript exit 0 

How It Works

Much of the work in this script takes place within an awk script, and it wouldn't be too much of a step to write the entire script in awk rather than in the shell, using the system() function to call df directly. This script would be an ideal candidate to rewrite in Perl, but that's outside the scope of this book.

There's also a trick in this script that comes from my early days of programming in BASIC, of all things:

 prettymb=(int(mb * 100)) / 100; 

When working with arbitrary-precision numeric values, a quick way to limit the number of fractional digits is to multiply the value by a power of 10, convert it to an integer (which drops the fractional portion), and then divide it back by the same power of 10. In this case, a value like 7.085344324 is turned into the much more attractive 7.08.


Some versions of df have an -h flag that offers an output format similar to this script's output format. However, as with many of the scripts in this book, this one will let you achieve friendly and more meaningful output on every Unix or Linux system, regardless of what version of df is present.

Running the Script

This script has no arguments and can be run by anyone , root or otherwise . To eliminate reporting space usage on devices that you aren't interested in, use grep -v after the call to df .

The Results

Regular df reports are difficult to understand:

 $  df  Filesystem              512-blocks     Used    Avail Capacity  Mounted on /dev/disk1s9              78157200 43187712 34457488    55%    / devfs                          196      196        0   100%    /dev fdesc                            2        2        0   100%    /dev <volfs>                       1024     1024        0   100%    /.vol /dev/disk0s9             234419552 71863152 162556416   30%     /Volumes/110GB 

The new script exploits awk to improve readability:

 $  newdf  Filesystem                     Size    Used   Avail Capacity  Mounted /dev/disk1s9                 37.26G  20.59G  16.43G      55%  / devfs                           98K     98K       0     100%  /dev fdesc                             1       1       0     100%  /dev <volfs>                        512K    512K       0     100%  /.vol /dev/disk0s9                111.77G  34.26G  77.51G      30%  /Volumes/110GB 

Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net