Steps in Writing a Script

A script is a kind of computer program.  There is a lot of information and many classes where you can study how to develop a program in a systematic way.   This is technically not a programming class, but it may be useful to review the programming process.   When I develop a script to solve a problem, I like to follow a step-by-step, iterative approach.  There are many tools that can be used.  One of my favorites is something called pseudocode.  Here is a short overview of flowcharts and pseudocode, and here is a  comparison of flowcharts and pseudocode.  I have written a slightly expanded version of version of the process with my comments below.   (If you are already comfortable with program development, then feel free to skip to the end of this section.  If you have a favorite summary that you would like to share, please post it or its URL to the class bulletin board).

You can think of the programming process as having five steps:

  1. define the problem
  2. design a solution
  3. write the script
  4. test it
  5. decide if you are done.  If you are not satisfied, then go back to a previous step.  This will usually be step 3 or step 2.  Sometimes you may end up going back to step 1 as well.
In order to define the problem, it is helpful if you know what the input is and what the output needs to be.  Then your job is just to figure out how to produce the desired output from the given input.   As you grow in experience and responsibility, usually you are given problems to work on with less clearly defined inputs and outputs.  In that case you will need to talk with your users (whether they are your boss, coworkers, or customers) to clarify the problem definition so that it is clear enough so that you can try to solve it.

The second step is to design a solution.  This too is an iterative process.  Here are the steps I usually follow:

  1. divide the problem into pieces.
  2. then break each piece into smaller pieces.
  3. do this repeatedly until each piece is small enough so that you can see how to translate it into a few Unix scripting commands.
  4. then review all the pieces and see if they will work together.  if not, then go through the process again.
Often I develop an initial solution using something called pseudocode.  These is when you write out the steps you would use to solve a problem in English-like statements.  You worry about the logic of the steps, not command syntax.  (This is the kind of thing you might write on the back of an envelope or on a napkin at a restaurant when an insight into the problem comes to you.)  Of course it has been often said that solving a problem is 1% inspiration and 99% perspiration.  The bulk of the work is usually in the implementation and testing of your design.

The third step is to write the script.  This is where you translate each piece of your design into corresponding Unix commands.  If you write scripts frequently, then you will want to develop a library of functions that you can use repeatedly to help you solve problems more quickly.

The fourth step is to test the script.    There are various kinds of tests:


The fifth step is to decide if you are done.  After a test you compare the output you get with the output you want.  If it matches, then you are done.  If not, then you analyze the differences and decide which step you need to go back to in order to fix it.

For simple problems, we might take most of these steps automatically.   I find it helpful to remember them for large or complex projects or whenever I get stuck.

Back To Top