Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [55]

By Root 928 0
in turn with a text editor. a2ps, tgrind, and vgrind can make listings of your programs, including shell scripts, easier to read.

Chapter 5. Pipelines Can Do Amazing Things

In this chapter, we solve several relatively simple text processing jobs. What's interesting about all the examples here is that they are scripts built from simple pipelines: chains of one command hooked into another. Yet each one accomplishes a significant task.

When you tackle a text processing problem in Unix, it is important to keep the Unix tool philosophy in mind: ask yourself how the problem can be broken down into simpler jobs, for each of which there is already an existing tool, or for which you can readily supply one with a few lines of a shell program or with a scripting language.

Extracting Data from Structured Text Files

Most administrative files in Unix are simple flat text files that you can edit, print, and read without any special file-specific tools. Many of them reside in the standard directory, /etc. Common examples are the password and group files (passwd and group), the filesystem mount table (fstab or vfstab), the hosts file (hosts), the default shell startup file (profile), and the system startup and shutdown shell scripts, stored in the subdirectory trees rc0.d, rc1.d, and so on, through rc6.d. (There may be other directories as well.)

File formats are traditionally documented in Section 5 of the Unix manual, so the command man 5 passwd provides information about the structure of /etc/passwd.[1]

Despite its name, the password file must always be publicly readable. Perhaps it should have been called the user file because it contains basic information about every user account on the system, packed together in one line per account, with fields separated by colons. We described the file's format in Section 3.3.1. Here are some typical entries:

jones:*:32713:899:Adrian W. Jones/OSD211/555-0123:/home/jones:/bin/ksh

dorothy:*:123:30:Dorothy Gale/KNS321/555-0044:/home/dorothy:/bin/bash

toto:*:1027:18:Toto Gale/KNS322/555-0045:/home/toto:/bin/tcsh

ben:*:301:10:Ben Franklin/OSD212/555-0022:/home/ben:/bin/bash

jhancock:*:1457:57:John Hancock/SIG435/555-0099:/home/jhancock:/bin/bash

betsy:*:110:20:Betsy Ross/BMD17/555-0033:/home/betsy:/bin/ksh

tj:*:60:33:Thomas Jefferson/BMD19/555-0095:/home/tj:/bin/bash

george:*:692:42:George Washington/BST999/555-0001:/home/george:/bin/tcsh

To review, the seven fields of a password-file entry are:

The username

The encrypted password, or an indicator that the password is stored in a separate file

The numeric user ID

The numeric group ID

The user's personal name, and possibly other relevant data (office number, telephone number, and so on)

The home directory

The login shell

All but one of these fields have significance to various Unix programs. The one that does not is the fifth, which conventionally holds user information that is relevant only to local humans. Historically, it was called the gecos field, because it was added in the 1970s at Bell Labs when Unix systems needed to communicate with other computers running the General Electric Comprehensive Operating System, and some extra information about the Unix user was required for that system. Today, most sites use it just to record the personal name, so we simply call it the name field.

For the purposes of this example, we assume that the local site records extra information in the name field: a building and office number identifier (OSD211 in the first sample entry), and a telephone number (555-0123), separated from the personal name by slashes.

One obvious useful thing that we can do with such a file is to write some software to create an office directory. That way, only a single file, /etc/passwd, needs to be kept up-to-date, and derived files can be created when the master file is changed, or more sensibly, by a cron job that runs at suitable intervals. (We will discuss cron in Section 13.6.4.)

For our first attempt, we make the office directory a simple text file, with entries like this:

Franklin,

Return Main Page Previous Page Next Page

®Online Book Reader