Wicked Cool Shell Scripts
345 pages. No Starch Press, 2004. $30
If youÃ¢â‚¬â„¢ve fiddled with the Terminal and the command line in Mac OS X a bit, you know how many commands are available, and how much power the Unix underpinnings of Mac OS X offer. While the command line offers no GUI – no buttons or menus – you can perform powerful tasks from the Terminal, and, in many cases, do things that the GUI does not allow.
Going further, shell scripts allow you to combine individual commands into small programs. For Mac users unfamiliar with the command line, shell scripts are similar to AppleScript, which lets you perform a series of tasks from a single script. But when you look at the vast number of tools available for shell scripting, AppleScript pales in comparison.Wicked Cool Shell Scripts, by Dave Taylor, who is both a Unix and Mac geek, is a compendium of cool shell scripts, giving powerful examples of the complex tasks you can carry out from the command line.
Now, shell scripting is not easy. There have been dozens of books written on the subject, most of which cover key commands (sed, awk, grep) in addition to presenting ways of gluing them together. But Taylor’s book takes a different tack: instead of teaching shell scripting, he presents 101 shell scripts, which are ideal examples of what you can do with these techniques.
While the book is not a tutorial, its scripts certainly are. Not only are they educational and informative, but the presentation of each script allows readers with a modicum of shell scripting knowledge to learn much more about what makes scripts work, and how to write them economically and efficiently. Each script is presented with an introduction describing the script and what it does, the actual code of the script (which you can download from the book’s companion website to avoid making typing mistakes), a “how it works” section, explaining what makes the script do what it does, a “running the script” section, which discusses any options or additional files required, and a “results” section, which presents the script’s output. An examination of these scripts, and especially the “how it works” sections, is a boon to anyone who has enough familiarity with the concept of shell scripting, but who finds writing long scripts difficult. Taylor’s explanations are concise, yet complete, and each of these sections is like a lesson in shell scripting.
As for the scripts themselves, many of them are wicked cool indeed. While only a handful of the scripts are specific to Mac OS X, most of the book’s scripts work on a Mac. One of the most valuable scripts for Mac users is the Adding a User to a Mac OS X System script, which lets you create new accounts from the command line. But there are many other interesting scripts, for manipulating files, formatting files, spell-checking, converting temperatures and performing calculations, managing and maintaining your system, administering web sites and servers, and even playing games.
From a Mac user’s point of view, I have only one gripe, and a minor one at that. Mac users buying this book might not be familiar enough with shell scripting to instinctively know that they need to change the permissions of shell scripts they create or download from the book’s web site. The command to run is the following:
chmod 755 [scriptname]
You then need to invoke the script as follows:
or place the script in a directory which is included in their PATH variable declaration, and call the script by typing its name.
But that’s the only gripe I have. While I’m proficient in basic shell scripting, I found tons of valuable examples in this book, as well as many useful scripts that I’ve added to my personal script library. If you’re interested in the command line and shell scripting, this is an excellent book which will extend your knowledge, and provide more than a hundred ready-to-run scripts.
Read more articles in this category: Mac OS X Command Line