Chapter 8. Automating Administrative Tasks

One of the greatest strengths of Mac OS X and Mac OS X Server is the abundance of scripting languages it supports out of the box. Many of these scripting languages are interpreted by a host program rather than run directly as lower-level machine code, and thus they are text files with human-readable syntax. Because such languages are translated into machine code at run time, interpreted programs are sometimes much slower than their compiled equivalents. However, because you can edit these programs and then run them immediately, they are common tools used by system administrators to automate tasks.

Some interpreters are specifically made to run code such as Python, Perl, or Ruby, while others are more interactive and are meant for day-to-day use, facilitating most of the command-line administration tasks covered in this book. Typically, this interactive interpreter component is referred to as a shell. The primary purpose of a shell is to translate commands typed at a terminal into some kind of system action. In other words, the shell is a program through which other programs are invoked.

There are several different Unix shells, including the C shell (csh), the Bourne shell (sh), and their more modern equivalents, tcsh and bash. In the most recent versions of Mac OS X, new users are assigned the bash shell as the default shell. In early versions of OS X the default user shell was tcsh, perhaps due to the presence of Wilfredo Sanchez on Apple's team. The former lead engineer for Mac OS X was also a developer of the tcsh shell. However, bash has proliferated through the various Linux distributions and has become one of the most prominent shell programs in use today. Perhaps recognizing this, Apple switched the default shell to bash in Mac OS X v.10.3, and it remains as such today in 10.6.

While the choice of a shell and its resultant scripting language can sometimes be difficult, we recommend you learn at least the basics of the bash shell before moving onto any other shell and language that may be better suited to your higher-level tasks. This is because, unlike with languages such as python or Perl that are more strictly used for scripting, you will typically use the bash shell every time you open up a terminal to run any command. The more comfortable you become with bash scripting, the more you may find yourself writing one-line scripts that allow you to automate even basic operations. For instance, every principle discussed in the "Scripting the Bash Shell" section of this chapter applies to the interactive environment presented when you fire up Terminal.

In this chapter, we present some basic building blocks you need to build your own complex automations. In the process, we attempt to show you some real world syntax examples of scripting in action. Hopefully by the end of this chapter, you'll be armed with enough knowledge to tackle the problems you face in your environment. We'd like to make a strong point at the outset: while you do not have to use the command line to be a good system administrator, most good system administrators do. This is because a simple operation, such as creating a series of folders, can be done using basic scripts, and in using these scripts you will find your administration becomes not only more efficient but also (and importantly in large environments) more consistent.

This chapter is not intended to provide in-depth coverage of all shells; that could be a book unto itself. This chapter will introduce you to scripting with bash and then supply some information on Perl for those who begin to outgrow the bash environment. We will walk through the basic constructs and control statements, providing a decent foundation for you to build on. Due to its default support in the latter iterations of Mac OS X, we will focus primarily on the bash shell syntax, but we will also include fully constructed examples of scripts using PHP, Perl, and python.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.82.79