Review: Wicked Cool Shell Scripts (book)
It’s been several years since the initial release of Mac OS X formally introduced Mac users to the world of Unix. Since then I’ve stumbled across a handful of Unix books and articles that claim to cover the Mac, and it often seems like the extent of Mac coverage can be summed up by the phrase, “By this way, this should work on OS X.”
That said, the subtitle for Wicked Cool Shell Scripts—“101 Scripts for Linux, Mac OS X, and Unix Systems”—made me slightly wary. I know you’re not supposed to judge a book by its cover (which, by the way, has a stylish retro thing going on), but a statement like that has a lot to live up to in my mind.
In spite of this, the book left me pleasantly surprised. With a few exceptions, the vast majority of the scripts in this book work under Mac OS X: those that don’t are noted and, in many cases, alternatives are provided in a Mac-specific chapter. Chalk that up as one promise kept.
One of the strengths of this book is its fairly consistent layout. Most of the 101 shell scripts come with detailed descriptions of how the interesting code portions work, sample input and output to verify that the scripts work, and ideas for further modifying the scripts to increase their functionality. Scripts that lack one or more of these sections are usually the extremely simple ones, which don’t require any explanation beyond the code.
When I started reading Wicked Cool Shell Scripts, I wasn’t filled with such warmth. While the book does jump directly into writing scripts, as opposed to first spending time teaching the scripting language, the first two chapters are rather bland. Validating input types? Finding tools accessible through the PATH variable? Converting DOS commands to their Unix counterparts? Ugh, wake me up when it’s over.
Truthfully, it took me longer to read the first two chapters than it took to read the remaining ten. That’s not to say these first two chapters aren’t worthwhile; if you haven’t done any shell scripting before, you have a chance to ease into the process with some shorter scripts.
The scripts in these chapters also act as building blocks for the rest of the book. In some cases, ideas from the early chapters will show up in more advanced scripts. In other cases, the more advanced scripts will simply run one of the earlier scripts.
If you can avoid jumping ahead, you’ll also be rewarded with a few diamonds in the rough from these introductory chapters. My personal favorites are the scripts that archive files when you remove them. Essentially, these scripts give you a command-line version of the Trash, as opposed to the rm command, which simply eradicates files from your hard drive.
Picking Up Steam
My impression of this book changed for the better once I got into chapter three and started working through the final ten chapters. The scripts in these chapters were often interesting or useful (or in some cases, both).
Chapters three and four both deal with creating and modifying Unix tools for various purposes, either making a tool more user-friendly or using a specialized tool to create a more generalized tool. A good example of this is the script that builds a spell checker, which uses both the ispell tool and your own customizable user dictionary.
The concept of using specialized tools inside of shell scripts is actually pretty important, and these chapters do a decent job of driving home this point.
The next two chapters deal with various aspects of system administration. If you’re the only person using your computer, a lot of these scripts won’t apply to you. Also, the Mac’s graphical user interface makes some of these redundant; for instance, who’s going to set the system date from the command-line when Apple provides a preference pane that can automatically do this over the Internet?
There are a couple of scripts that anybody who dips into the command-line environment could find useful, though. There’s one script for checking entire crontab files for errors, instead of waiting to find out about errors individually, and another script for managing log files before they get too unwieldy.
Of course, if you manage Macs in a multi-user environment, you could learn even more from these sections. I don’t run any labs, so I’m not really in a position to judge this.
The system administration chapter is where most of the Mac-incompatible scripts can be found. The blatantly incompatible scripts are those that deal with adding, deleting, and modifying users, as OS X uses a completely different user management system from any other Unix operating system.
You should also be careful if you decide to use either of the backup scripts provided in these chapters. Both scripts use standard Unix file compression tools, which will not preserve any resource forks (or other Mac metadata) that might be present in your files.
Networking and the Internet
The scripts in chapters seven through ten all deal with various networking and Internet-related tasks, starting with accessing the Internet and working up to server administration.
The majority of the scripts for Internet users deal with gathering content from Web sites, for instance movie information from the IMDb, stock information from Lycos, or headlines from BBC News. As these sites change their designs, the scripts will likely break, but the ideas behind the scripts are still valid.
Chapter eight covers scripts, many in the form of CGIs, for webmasters. This section includes examples of common Web elements like counters, photo albums, and guest books. Few of these scripts result in the most sophisticated Web pages, but they do provide a solid basis for future growth. For instance, Dave Taylor’s own Web site incorporates a fully-featured photo album built with shell scripts.
Putting your Web site on the Internet is only the first step; after that, you need to keep it up and running. Fortunately, chapter nine features a number of scripts for maintaining your Web site, from those that check your spelling and links to those that synchronize your local and remote files by FTP or SFTP.
The final Internet-related chapter deals with actual server administration. Topics covered include keeping your server running smoothly and monitoring information about who is viewing your Web site.
The Good Stuff
Here’s the part of the book that justifies including “Mac OS X” in the subtitle: a half dozen scripts that won’t work on any Unix system other than OS X (although there is one that might work under Windows, with some coaxing). Linux users don’t get Linux-only scripts, BSD users don’t get BSD-only scripts. Just us OS X users.
The first three of these scripts deal with the NetInfo database that OS X uses to store all of its user data. There’s a script for listing all of the user accounts present on the system (including the accounts that don’t show up in the login panel), there’s a script for adding new users, and there’s a script for setting up e-mail aliases if you’re using your Mac as a mail server.
Granted, NetInfo Manager provides a graphical interface for doing all of these things, but this might be one of those rare cases where using the command-line is actually more user-friendly than a graphical application. If you haven’t had the opportunity to use NetInfo Manager, let’s just say it’s not the best example of user interface design.
The fourth script is a quickie that dynamically sets the title of your Terminal window. On the one hand, it’s a bit of a gimmick. However, if you like having a short command prompt (like me), this lets you display the current working directory in the window’s title bar.
The fifth script deals with iTunes. Don’t get too excited about this one; it simply reads your iTunes Music Library.xml file and generates human-readable output. The output from the script is less dense, and more legible, than the output from iTunes’ “Export Song List…” option.
The final OS X script is a customizable replacement for the open tool. This newer version lets you create shortcuts for applications so you can do things like launch QuickTime Player by typing open2 qt instead of open -a QuickTime\ Player.
There’s also a bonus script that shows you how to take a screenshot every minute, using the screencapture tool. It doesn’t count towards your quota of 101 shell scripts, but you get what you pay for, since the script implies that screencapture generates TIFF files instead of PDFs (Apple switched to using the PDF format as of Mac OS X 10.2).
Taking a Breather
If you make it to chapter 12, you’re rewarded with games in the form of the final three shell scripts: a Jumble clone, a Hangman clone, and a state capital quiz. No Quake here, but a nice reminder that shell scripts can be used for a number of different things, not just system tools.
Wicked Cool Shell Scripts isn’t perfect. As a selfish Mac user, I’d certainly like more Mac-specific content, perhaps a backup script that preserves resource forks by creating a compressed disk image instead of using tar. A few examples of shell scripts using the osascript command, which lets you run AppleScript from the command-line, would have been nice.
I noticed a few typos in some of the code included in the book. There wasn’t anything major and some of the problems were fixed on the book’s Web site, but it was a little annoying.
The other issue I had with the code was with the choice of font. The big problem was that the font used made it hard to distinguish a zero (0) and O (capital-oh). I’m a firm believer that any code examples should use a font that places a slash through the center of the 0, as it makes the code much more readable.
Minor quibbles aside, I thoroughly enjoyed reading this book. By the time I was finished, I had ideas for expanding some of the scripts in the book, ideas for adapting some of the scripts for other tasks, and ideas for writing completely new scripts. I’d say that any book capable of motivating people three-fold will prove quite successful.