Automating frequent tasks

Writing a readability analysis program: an example

For the rest of this chapter, and at intervals in the following chapters, we will refer to a single recurring example: a program to analyze the readability of text files. Such a program needs to identify the files it is to work on. It must open them, use several other programs to obtain information about the files, then print the results. It also serves as a demonstration of several useful techniques: notably, how to built a simple menu driven program, how to build up complex regular expressions, and how to integrate awk scripts and other programming languages into shell programs.

The objective of a readability analysis program is to scan a file or files of text, and report various statistics about their internal complexity. There is more to this than just running wc; we want to generate a report on such things as the number of sentences in a file, the average length of each sentence, the average number of syllables per word, and the readability grade of the file. It would be useful to be able to invoke the program from the shell prompt with a variety of options: it would also be useful to provide the program with a menu driven front end. All these tasks, and more, will be explained as we encounter them in building up our example.

The first step in writing a large program is to analyze what it is intended to do: what its inputs are, and what its outputs are expected to be. We can then write a ``skeleton'' for it: a script that does not actually do anything to the data, but ensures that all the pieces are in place. (The actual task of analyzing a file for readability can be farmed out to a function that we will fill in later.) This is described below.

Next topic: How to structure a program
Previous topic: What to do if your shell script fails

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003