Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Validating User Input

Say that you need to write a script that needs to ask the user for the name of a directory. You can use the following steps to get information from the users:

1.  Ask the user a question.
2.  Read the user’s response.
3.  Check to see whether the user responded with the name of a directory.

What should you do when the user gives you a response that is not a directory?

The simplest choice would be to do nothing, but this is not very user friendly. Your script can be much more user friendly by informing the user of the error and asking for the name of a directory again.

The while loop is perfect for doing this. In fact, one of the most common uses for the while loop is to check whether user input has been gathered correctly. Usually a strategy similar to the following is employed:

1.  Set a variable’s value to null.
2.  Start a while loop that exits when the variable’s value is not null.
3.  In the while loop, ask the user a question and read in the users response.
4.  Validate the response.
5.  If the response is invalid the variable’s value is set to null. This enables the while loop to repeat.
6.  If the response is valid, the variable’s value is not changed. It continues to hold the user’s response. Because the variable’s value is not null, the while loop exits.

In the following example, use the commands

  echo to display a string
  read to read in the user’s response

I have not formally introduced these commands, but you might be familiar with them. For readers who are not familiar with these commands, I’ll cover them in Chapter 13, “Input/Output.”

A while loop follows that solves your problem:

RESPONSE=
while [ -z "$RESPONSE" ] ;
do
    echo "Enter the name of a directory where your files are
    ⇒located:\c "
    read RESPONSE
    if [ ! –d "$RESPONSE" ] ; then
        echo "ERROR: Please enter a directory pathname."
        RESPONSE=
    fi
done

Here you store the user’s response in the variable RESPONSE. Initially this variable is set to null, enabling the while loop to begin executing.

When the while loop first executes, the user is prompted as follows:

Enter the name of a directory where your files are located:

The user can type the name of a directory at this prompt. When the user finishes typing and presses Enter, the read command puts the user’s input into the variable RESPONSE. You then check to make sure the input is a directory. If the input is not a directory, issue an error message and the loop repeats. The error message is produced so that the user knows what was wrong with the input.

If the user does not enter any value, the variable RESPONSE is still set to null. In this case the value stored in the variable RESPONSE is not a directory, thus the error message is produced.

The until Loop

The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.

A variation on the while loop available only in ksh and bash, the until loop provides this functionality. Its basic syntax is:

until command
do
    list
done

Here command is a single command to execute, whereas list is a set of one or more commands to execute. Although command can be any valid UNIX command, it is usually a test expression of the type covered in the last chapter.

The execution of an until loop is identical to that of the while loop and proceeds according to the following steps:

1.  Execute command.
2.  If the exit status of command is nonzero, exit from the until loop.
3.  If the exit status of command is zero, execute list.
4.  When list finishes executing, return to Step 1.

If both command and list are short, the until loop can be written on a single line as follows:

until command ; do list ; done

In most cases the until loop is identical to a while loop with list1 negated using the ! operator. For example, the following while loop

x=1
while [ ! $x -ge 10 ]
do
    echo $x
    x=`echo "$x + 1" | bc`
done

is equivalent to the following until loop:

x=1;
until [ $x -ge 10 ]
do
    echo $x
    x='echo "$x + 1" | bc'
done

The until loop offers no advantages over the equivalent while loop. Because it isn’t supported by all versions of the Bourne shell, programmers do not favor it. I have covered it here because you might run across it occasionally.


Previous Table of Contents Next