Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
![]() |
Home ![]() ![]() ![]() |
||||||||||||||
See also: How do I write a simple shell script?, Example Bash script | ||||||||||||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
||||||||||||||
Developing good scripts
StructureThe 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:
TerminologyThe table below gives an overview of programming terms that you need to be familiar with: Table 1-1. Overview of programming terms
A word on order and logicIn 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:
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.
|
||||||||||||||
Home ![]() ![]() ![]() |