We have the user-friendly AppleScript and now Automator. But long before either of these existed, there were shell scripts —series of Unix commands saved as mini-programs. By learning how to write these you gain expertise that’s directly applicable to Linux and other Unix systems. Not only will you be able to use your knowledge in non-Mac environments, but you’ll have some serious Geek cred. So if you’re tired of typing command after command in Terminal, to it might be time to give shell scripts a try.
Unix unleashed
Shell scripts let you link commands together. Pipes (|) are the key—these connect the output of one command to the input of the next. For example, the ls command lists the files in your current folder ( directory in Unix-speak) and the wc -l command counts number of lines. Graft these two commands together to find out how many files are in the directory. It’s as simple as opening Terminal (/Applications/Utilities) and typing
ls | wc -l.
Because of the pipe, the output from the file list command ( ls ) is received as input to the word count ( wc ) command, which then tallies up the total number of files. Drop those two commands into a text file, give it a name with the extension .sh (for example, file-count.sh) and you’ve written your first simple shell script.
Shell scripts can be significantly more sophisticated, complete with loops, conditional statements, and other programming do-dads. Here I’ll give you a peek at shell scripts’ potential by looking at a cool script that plugs into Tiger’s power. Don’t be intimidated—you don’t have to type in shell scripts to use them! Download this one and save it to your home folder. To run a shell script, type
sh scriptnamefrom within Terminal.
Capture a series of screenshots
Press Command-Shift-3 and you capture an image of your current screen to a file on the desktop. But what if you want to capture a sequence of screenshots, or want to have a handy recording device to ensure no one’s touching your computer while you’re at lunch? Tiger offers a free and straightforward solution—the command screencapture . (For detailed information about screencapture ’s abilities, type
screencapture –hin Terminal.)
To capture a single screen shot, type
screencapture myscreen. The command will create a PNG file with the current screen contents and put it in the current directory, probably your home folder. (If you can’t open this image, it may be missing an extension. Add .png to the end of its name.) To capture a series of screenshots, use the shell script I call screen-record.sh. It looks like this:
count=1 while [ true ] ; do filename="$basefilename-$count" screencapture "${filename}.png" sleep 60 # 60 seconds count=$(( $count + 1 )) done
Basically, the script saves whatever base filename you specify when you start the program, then takes a screen capture, sleeps for 60 seconds, adds one to the “count” variable, and then does it all again. This script will run as long as you let it: press Ctrl-C in Terminal to make it stop.
You must specify a basefilename when you start the script. For example, type something like
sh screen-record.sh mydesktop. Each screenshot will be named basefilename plus count plus the filename suffix (for example, mydesktop-5.png ).
The count is a separate variable that increases as the script runs. I’ve written the script to have a 60 second delay between shots, but that could easily be changed to 300 for an every-five-minute capture, or even 1800 if you only want a screen capture every half hour. When you download the final script, you’ll see I added a few more bells and whistles—mostly error-checking tests.
[ Dave Taylor is a 25 year Unix veteran and author of Wicked Cool Shell Scripts (No Starch Press, 2004) and Learning Unix for Mac OS X Tiger , (O’Reilly, 2005). ]
EDITOR’S NOTE: Intrigued by shell scripting? Download a sample PDF chapter from Michael Trent and Drew McCormack’s Beginning Mac OS X Programming to get started. It is reprinted from Beginning Mac OS X Programming by Michael Trent and Drew McCormack with the permission of Wiley Publishing, copyright 2005. For more information on this or other books visit Wrox Press.