|Lesson 2||Defining a script objective |
|Objective||Formulate a clear objective for writing a script.|
Why define an Objective?
Sometimes you will sit down to write a shell script with nothing more in mind than,
"I will solve this problem with a shell script."
Rarely does that statement alone lead to an efficient solution to a complex problem. What you need is an objective or specific goal to help you outline what problem needs to be solved and how a shell script will solve it. Once you’ve formulated an objective, you’re halfway to creating an outline of the script itself. Of course, finding the right command options, testing, and debugging the script will take time. But once a clear objective has been set down, it’s much easier to design your shell script.
The following paragraph describes why it is important not to write fuzzy objectives.
When you start diagramming and then writing your script, you'll be able to see if your objective was not detailed enough. The overall picture of how the script functions will be fuzzy in your mind. Rather than just wondering about how to complete specific commands, you'll find yourself asking "can I even do this in a shell script?" Working on the objective beforehand can save you from hours of work on a project that ends up being discarded.
How to write an objective
A programming objective for your shell script should state both what you intend to accomplish and how you will accomplish it.
Here is an example of a flawed objective statement for a project at the Far Away Travel Agency: “Use a shell script to convert the existing database records to the new system. This statement is inadequate as it leaves us asking several questions. What records will be converted? What does a user need to know or do for the conversion to happen? Are any other programs required?
A better-formulated version of this objective might read something like this: Use a shell script to convert existing records (from the customer service call archive and the worldwide hotel information archive) from the current database system to the new system using the
dbconvert command line utility provided with the new database. An ID code for each file must be provided by the user during the conversion process.”
This restated objective tells us three important facts about the script to be written:
- Another program (dbconvert) will do the conversion. The shell script will manage the process.
- The person running the script will need additional information (an ID code) to enter for the file.
- Two specific database tables are the subject of this shell script.
The objective has therefore given us a clear sense of what we will need to design and write this shell script. By knowing these things as we start writing the script, we can keep the following questions in mind:
- Do these two database tables have special properties that require attention in the script?
- Can we generalize the script so that it might apply to other database tables later on?
- What form does the ID code take? How does the user of the script know what to enter and how can the script check the user entry for correctness?
- Does the dbconvert utility use STDIN and STDOUT, which may make programming tasks easier in the shell script?
We may not know all the answers to these questions immediately. But if we have valid, focused questions, our script will not contain obvious contradictions to these issues. In the next lesson we discuss diagramming the flow of a script.