Linux Know-How provides a collection of introductory texts on often needed Linux skills.


Developing good scripts

This guide is mainly about the last shell building block, scripts. Some general considerations before we continue:

  1. A script should run without errors.

  2. It should perform the task for which it is intended.

  3. Program logic is clearly defined and apparent.

  4. A script does not do unnecessary work.

  5. Scripts should be reusable.

Structure

The structure of a shell script is very flexible. Even though in Bash a lot of freedom is granted, you must ensure correct logic, flow control and efficiency so that users executing the script can do so easily and correctly.

When starting on a new script, ask yourself the following questions:

  • Will I be needing any information from the user or from the user's environment?

  • How will I store that information?

  • Are there any files that need to be created? Where and with which permissions and ownerships?

  • What commands will I use? When using the script on different systems, do all these systems have these commands in the required versions?

  • Does the user need any notifications? When and why?

Terminology

The table below gives an overview of programming terms that you need to be familiar with:

Table 1-1. Overview of programming terms

TermWhat is it?
Command controlTesting exit status of a command in order to determine whether a portion of the program should be executed.
Conditional branchLogical point in the program when a condition determines what happens next.
Logic flowThe overall design of the program. Determines logical sequence of tasks so that the result is successful and controlled.
LoopPart of the program that is performed zero or more times.
User inputInformation provided by an external source while the program is running, can be stored and recalled when needed.

A word on order and logic

In order to speed up the developing process, the logical order of a program should be thought over in advance. This is your first step when developing a script.

A number of methods can be used; one of the most common is working with lists. Itemizing the list of tasks involved in a program allows you to describe each process. Individual tasks can be referenced by their item number.

Using your own spoken language to pin down the tasks to be executed by your program will help you to create an understandable form of your program. Later, you can replace the everyday language statements with shell language words and constructs.

The example below shows such a logic flow design. It describes the rotation of log files. This example shows a possible repetitive loop, controlled by the number of base log files you want to rotate:

  1. Do you want to rotate logs?

    1. If yes:

      1. Enter directory name containing the logs to be rotated.

      2. Enter base name of the log file.

      3. Enter number of days logs should be kept.

      4. Make settings permanent in user's crontab file.

    2. If no, go to step 3.

  2. Do you want to rotate another set of logs?

    1. If yes: repeat step 1.

    2. If no: go to step 3.

  3. Exit

The user should provide information for the program to do something. Input from the user must be obtained and stored. The user should be notified that his crontab will change.


Last Update: 2010-12-16