GeekTool is a Mac OSX Application (or System Preference Pane) that lets you display various kinds of information on your desktop via 3 default plug-ins. GeekTool is a software for Mac OS X that lets you display on your desktop all kinds of informations using log files, shell scripts or images.
awk '{printf('%.2f%%', $10/$5 * 100)}'
This first awk command reads the standard input, one line at a time (if necessary; I'm guessing only one line is being sent to it here), and splits the line into columns, the data in column c having the name $c. (So $5 is the 5th column's data and $10 is the 10th column's). The printf command calculates $10/$5*100 (that is, the 10th column divided by the 5th column, times 100) and prints it as a floating-point number with 2 decimal places ('%.2f') followed by a percentage sign ('%%').
awk '$3~/Capacity/ {c[$3]=$5} END{OFMT='%.2f%%'; max=c['MaxCapacity']; print(max>0?100*c['CurrentCapacity']/max:'?')}'
This awk command starts by reading in each line of ioreg -l, one at a time. Whenever column 3 ($3) contains the word 'Capacity' ($3~/Capacity), it runs the command 'c[$3]=$5', which stores the fifth column into an array (a box) labelled with the contents of the third column. (Unlike in some languages, awk's arrays can be labelled with text, not just numbers.)
The END segment means to run the following command after you're done reading in standard input. First it sets OFMT='%.2f%%', which means to change the output format so that it outputs real numbers with two decimal places followed by a percentage sign (as above). Second, it defines the variable max to be the contents of the array labelled 'MaxCapacity' (which was defined in the first part of the process). Next it prints out the result of the formula max>0? 100*c['CurrentCapacity']/max: '?'
which is an if-then statement (with the form test?true:false; awk stole this notation from C). If the variable max is greater than zero, then take the contents of the array labelled 'CurrentCapacity', divide it by max, and multiply by 100. If max is not greater than zero, then something wrong happened (maybe ioreg doesn't have a line which says 'MaxCapacity') and so return the character '?'.
Geektool Time
Type 'man awk' in the terminal for a full description of the command. It's basically a full-blown programming language, designed to deal with text files one line at a time, and it's really useful; it's also something you can pick up a little bit at a time, if you're so inclined.
In this week’s video, I look at GeekTool, a free system utility that lets you display images, text files, and the output of Unix commands on your desktop (or floating above other windows).
Download Macworld Video #106
• Format: MPEG-4/H.264
• Resolution: 480 x 272 (iPhone & iPod compatible)
• Size: 13.3MB
• Length: 7 minutes
Geektool
In the course of the video, I reference a collection of sites where you can find more powerful examples of what you can do with GeekTool. These include:
- Hints that mention GeekTool on macosxhints.com.
- Ultimate GeekTool Setup on Keynote 2 Keynote.
- GeekTool and bash one liners on Mac Geekery.
- Geek to Live: Monitor your Mac and more with GeekTool on Lifehacker.
- Mega Roundup of GeekTool Scripts on Francesco Mugnai’s blog (which contains links to many other sites).
- In the video, I show one GeekTool entry that displays my external IP address, which can be useful if you’re trying to debug a network connectivity problem. The Unix source for that entry can be found buried in this hint on Mac OS X Hints. To save you the digging, though, here’s the command:
curl --silent http://checkip.dyndns.org | awk '{print $6}' | cut -f 1 -d '<'
. Just enter that as the Unix command, and you should see your external IP address in GeekTool.
To subscribe to the Macworld Video Podcast using iTunes 5 or later, click here.
You can also see a complete archive of all our videos on Macworld’s YouTube channel. Subscribe to that channels and you will be notified whenever we post a new video.
Geektool Blog
Or just point your favorite podcast-savvy RSS reader to: http://feeds.macworld.com/macworld/video/