User Interaction

Scripts should be designed, whenever possible, to perform correctly without user intervention but there are cases where user intervention is appropriate. The following example illustrates one bash shell method that only requires user input when default values are wrong.

MAC=00:B0:52:00:BA:BE
echo -n "MAC Address [${MAC}]: "; read
if [ ! -z ${REPLY} ]; then
        MAC="${REPLY}"
fi

First, we define symbol MAC with a default value. The Linux echo utility prints a prompt on the console that includes the symbol value. The trailing newline is suppressed (-n) so that text can be typed immediately after the prompt. The echo command is terminated with semicolon (;) so that another command can be included on the same line. The shell read statement waits for the user to type something and press the enter key. The shell will assign the input to shell variable REPLY. The value of REPLY is evaluated and used to redefine the symbol only if the input was a non-zero length string.

MAC Address [00:B0:52:00:BA:BE]: 

The user will see something like this. If the value is correct the user can press the enter key to generate a zero length string. Otherwise, the user can type the correct value before pressing the enter key.