Chapter 6. Advanced Scripting

In the past three chapters, you've learned how to work with Terminal, use basic shell commands, organize some commands into simple scripts, work with files, and manipulate text.

In this chapter, you're going to learn how to create some more advanced shell scripts. You'll also learn the basics of AppleScripting. Finally, you'll be introduced to the open command, a powerful tool that enables you to open files, Finder windows, and applications such as iTunes and Word straight from the command line. The goal of this chapter is to make you more proficient in automating certain administrative functions. Think of this chapter as a summary of what you've already learned, and a necessary step toward success in more advanced topics, which we'll cover from this chapter through Chapter 14.

Shell Scripting on Mac OS X

You got a taste in Chapter 3 of what shell scripts are about. At its most basic, a shell script is nothing more than a series of commands that have been saved into an executable file.

For example, if you are monitoring the server load on a particular machine, you might use the uptime command, like so:

uptime
14:28  up 2 days, 40 mins, 2 users, load averages: 0.08 0.51 0.45

The response is fairly expected—current time, how long the machine has been up, how many users are connected to it, and load averages over the past 1, 5, and 15 minutes.

What if this output is too much for ...

Get MAC OS® X UNIX® Toolbox: 1000+ Commands for the Mac OS® X now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.