Unix Commands  «Prev  Next»
Lesson 5 Data handling errors
Objective Describe how to handle incorrect user input.

Data handling errors (incorrect user input)

Data handling errors occur when a user enters incorrect or unexpected input.
These are notreally errors because your program will still work if your user enters the expected input. Data handling errors do not show up until the program is run later using the bad input. This might include a user typing a space where it does not belong or typing a word when your program is expecting a number. When writing your shell script, decide how much input checking to include in your code. You can decide to:
  1. Allow the program to fail if a user enters bad input. This saves you time programming, but aggravates the user.
  2. Check for some of the possible bad inputs, but not all.
  3. Handle every possible bad input. This saves the user aggravation, but uses a lot of programming time.
This lesson listed your choices for handling the situation. We will look at specific examples of handling bad user input later in this module.
The next lesson examines two common problems with user input, and how write your code to work around these problems.

Unix Shell Programming

Standard Error

In addition to standard input and standard output, there is a third virtual device known as standard error. This is where most Unix commands write their error messages. And as with the other two standard places, standard error is associated with your terminal or terminal app by default. In most cases, you never know the difference between standard output and standard error:
$ ls n* List all files beginning with n
n* not found
$

Here the "not found" message is actually being written to standard error by the ls command. You can verify that this message is not being written to standard output by redirecting the ls command’s output:
$ ls n* > foo
n* not found
$