Previous | Table of Contents | Next |
Prompting for a Response
In many shell scripts, you need to gather more information from the user than a yes or no response. For example, installation scripts frequently have to ask for the name of a directory, the location of a file, or other system information.
The promptYESNO function cannot handle these types of questions. You need a different kind of prompting function, which you present as the promptRESPONSE function. This function displays a prompt, reads the users response, and stores it in the variable RESPONSE. Validation of the users response needs to be handled outside of the function.
################################################ # Name: promptRESPONSE # Desc: ask a question # Args: $1 -> The prompt # $2 -> The default answer (optional) # Vars: RESPONSE -> set to the users response ################################################ promptRESPONSE() { if [ $# -lt 1 ] ; then printERROR "Insufficient Arguments." return 1 fi RESPONSE="" DEF_ARG="$2" while : do printf "$1 ? " if [ -n "$DEF_ARG" ] ; then printf "[$DEF_ARG] " fi read RESPONSE if [ -n "$RESPONSE" ] ; then break elif [ -z "$RESPONSE" -a -n "$DEF_ARG" ] ; then RESPONSE="$DEF_ARG" break fi done export RESPONSE unset DEF_ARG return 0 }
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 users 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:
In which directory do you want to install ?
If you enter the name of a valid directory, no further prompts are generated; otherwise, you are asked whether the directory should be created:
The directory mydir does not exist. Create it (y/n)? [y]
Here the default is to create the directory. You can modify the example slightly to provide a default directory as follows:
promptRESPONSE "In which directory do you want to install" "$HOME"
The prompt changes to look like the following:
In which directory do you want to install ? [/home/ranga]
Previous | Table of Contents | Next |