Web space - } Before you look at some examples of
} Before you look at some examples of this function in use, examine how it works. This function is quite similar to the promptYESNO function. As indicated by the comments, promptRESPONSE can handle up to two arguments. It treats the first argument as the prompt and the second argument as the default answer to the prompt. The first thing this function checks for is that at least one argument is given. If no arguments are given, you return from the function with the following error message: ERROR: Insufficient Arguments. Next you set the variable RESPONSE to null to avoid using a value stored in it from a previous call to this function. After this, you set DEF_ARG, the default answer, to the value of the second argument of the function, $2. If the second argument is not given, DEF_ARG is set to null, because of variable substitution. At this point, you enter the body of an infinite while loop. You call the break command from inside the while loop after the user has entered a valid answer. The first thing the loop does is display a prompt using the printf command. You use the printf command to avoid problems with the echo command between different versions of UNIX. If a valid default answer was specified, you display it out. After the prompt is displayed, you call the read command and read the user’s response into the variable RESPONSE. If the user entered a value, you call the break command to exit the while loop. If the user simply presses Enter, RESPONSE is set to null. In this case, you check to see whether DEF_ARG contains a default answer. If it does, you set RESPONSE to this value and call the break command to exit the while loop. This behavior is similar to the promptYESNO function. If the default argument was not given and the user presses Enter, the prompt is displayed again. After a valid response is given, the while loop terminates. When this happens, the function exports the variable RESPONSE to the environment and returns by calling the return command. Now that you know how this function works, look at an example of its use. The following set of commands could be used in an install script: promptRESPONSE “In which directory do you want to install” if [ ! -d “$RESPONSE” ] ; then echo “The directory $RESPONSE does not exist.” promptYESNO “Create it” “y” if [ “$YESNO” = “y” ] ; then mkdir “$RESPONSE” else exit fi fi At first you are prompted as follows:
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.