Design Scripts  «Prev  Next»
Lesson 4Deciding on tests and loops
ObjectiveDetermine which tests and loops fit the needs of a script.

Determine Tests and loops for Shell Script

A script outline or diagram makes it easy to identify which tests and loops are needed to make your script behave as you want it to. As you will see in future modules, dozens of tests and loop statements are available.

Reviewing Script Tests

The outline in the previous lesson showed three places where a test or verification is needed. In preparing to write our script, we also need to determine what type of test is needed and when it should be applied. This helps preclude problems from arising once the script is written, such as testing for the wrong type of data. In the outline created earlier, we included three kinds of tests or verification.

Shell Checklist
Shell Checklist

  1. This test, the first one needed, checks for "valid data" on the command line. We must decide what constitutes valid data. Do we want to see if the data is a valid filename? Do we need to test whether the file exists? Can we do that later in the script? Which is better and why?.
  2. This test is a true/false test (a Boolean test) to see if the file that the script will try to convert exists as named on the command line. Here we can decide exactly which type of test to perform. Is the file a symbolic link? What if it is the name of a directory? What if we don't have permission to read the file?
  3. This test, the third test in the outline above, looks for a possible error from another program. In this case, the outline indicates that we should write an error message and exit the script. Do we still log anything to the log file? Can we determine the status of the converted file from the dbconvert error code? Should the test continue with the next file or assume all files will have errors if this one causes an error?

  1. Check for valid data on the command lineand if none is provided, print a usage message.
  2. This test, the first one needed, checks for “valid data” on the command line. We must decide what constitutes valid data. Do we want to see if the data is a valid filename? Do we need to test whether the file exists? Can we do that later in the script? Which is better and why?
  3. Check whether the file exists.
  4. This test is a true/false test (a Boolean test) to see if the file that the script will try to convert exists as named on the command line. Here we can decide exactly which type of test to perform. Is the file a symbolic link? What if it is the name of a directory? What if we do not have permission to read the file?
  5. If dbconvert causes an error, write the error and exit the script.
  6. This test, the third test in the outline above, looks for a possible error from another program. In this case, the outline indicates that we should write an error message and exit the script. Do we still log anything to the log file? Can we determine the status of the converted file from the dbconvert error code? Should the test continue with the next file or assume all files will have errors if this one causes an error?

Reviewing Looping Statements

The outline in the previous lesson also included a loop. For this loop, we must answer the following questions: How many times should the loop run? Should the loop exit automatically if any error occurs? Should other situations cause the loop to exit? Should the loop run at least once or might it not run at all? You see that many questions must be answered to create a script that covers all the possible situations.
The point here is not to answer all the questions, but rather to show you how to ask them.
.
A small script can be written by an experienced programmer without going through this entire process and make additions as they are needed. But when you are new to programming, or when others execute your shell scripts, it is best to ask many questions in the beginning--this will lead to reliable, full-function scripts. The next lesson teaches you how to test commands before they become part of your script.