Previous | Table of Contents | Next |
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:
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:
In the following example, use the commands
I have not formally introduced these commands, but you might be familiar with them. For readers who are not familiar with these commands, Ill 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 users 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 users 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 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:
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 isnt 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 |