This book is organized into 12 chapters that reflect the wide range of different ways that shell scripts can improve and streamline your use of Unix. If you're a Mac OS X fan ” as I am ” rest assured that almost every script in this book will work just fine in both Jaguar and Panther, with the exception of those scripts that check the /etc/passwd file for account information. (The user password information is in the NetInfo database instead. Visit the book's website for a discussion of this issue and how to work with nireport and nidump instead.)
Programming languages in the Unix environment, particularly C and Perl, have extensive libraries of useful functions and utilities to validate number formats, calculate date offsets, and perform many more useful tasks . When working with the shell, you're left much more on your own, so this first chapter focuses on various tools and hacks to make shell scripts more friendly, both throughout this book and within your own scripts. I've included various input validation functions, a simple but powerful scriptable front end to bc , a tool for quickly adding commas to improve the presentation of very large numbers , a technique for sidestepping Unixes that don't support the helpful -n flag to echo , and an include script for using ANSI color sequences in scripts.
These two chapters feature new commands that extend and expand Unix in various helpful ways. Indeed, one wonderful aspect of Unix is that it's always growing and evolving, as can be seen with the proliferation of command shells . I'm just as guilty of aiding this evolution as the next hacker, so this pair of chapters offers scripts that implement a friendly interactive calculator, an unremove facility, two different reminder/event-tracking systems, a reimplementation of the locate command, a useful front end to the spelling facility, a multi-time-zone date command, and a new version of ls that increases the use-fulness of directory listings.
This may be heresy, but there are aspects of Unix that seem to be broken, even after decades of development. If you move between different flavors of Unix, particularly between open source Linux distributions and commercial Unixes like Solaris and HP-UX, you are aware of missing flags, missing commands, inconsistent commands, and similar issues. Therefore, this chapter includes both rewrites and front ends to Unix commands to make them a bit friendlier or more consistent with other Unixes. Scripts include a method of adding GNU-style full-word command flags to non-GNU commands and a couple of smart scripts to make working with the various file-compression utilities considerably easier.
If you've picked up this book, the odds are pretty good that you have both administrative access and administrative responsibility on one or more Unix systems, even if it's just a personal Debian Linux or FreeBSD PC. (Which reminds me of a joke: How do you fix a broken Windows PC? Install Linux!) These two chapters offer quite a few scripts to improve your life as an admin, including disk usage analysis tools, a disk quota system that automatically emails users who are over their quota, a tool that summarizes which services are enabled regardless of whether you use inetd or xinetd , a killall reimplementation, a crontab validator, a log file rotation tool, and a couple of backup utilities.
If you've got a computer, you've also doubtless got an Internet connection. This chapter includes a bunch of really cool shell script hacks that show how the Unix command line can offer some wonderful and quite simple methods of working with the Internet, including a tool for extracting URLs from any web page on the Net, a weather tracker, a movie database search tool, a stock portfolio tracker, and a website change tracker with automatic email notification when changes appear.
The other side of the web coin, of course, is when you run a website, either from your own Unix system or on a shared server elsewhere on the network. If you're a webmaster or an ISP, the scripts in this chapter offer quite interesting tools for building web pages on the fly, processing contact forms, building a web-based photo album, and even the ability to log web searches. This chapter also includes a text counter and complete guest book implementation, all as shell scripts.
These two chapters consider the challenges facing the administrator of an Internet server, including two different scripts to analyze different aspects of a web server traffic log, tools for identifying broken internal or external links across a website, a web page spell-check script, and a slick Apache web password management tool that makes keeping an .htaccess file accurate a breeze . Techniques for mirroring directories and entire websites with mirroring tools are also explored.
The Macintosh operating system is a tremendous leap forward in the integration of Unix and an attractive, commercially successful graphical user interface. More importantly, because every Mac OS X system includes a complete Unix hidden behind the pretty interface, there are a number of useful and educational scripts that can be written, and that's what this chapter explores. In addition to a rewrite of adduser , allowing new Mac OS X user accounts to be set up in seconds from the command line, scripts in this chapter explore how Macs handle email aliases, how iTunes stores its music library, and how to change Terminal window titles and improve the useful open program.
What's a programming book without at least a few games? This last chapter integrates many of the most sophisticated techniques and ideas in the book to present three fun and challenging games. While entertaining, the code for each is also well worth studying as you read through this last chapter. Of special note is the hangman game, which shows off some smart coding techniques and shell script tricks.