< Day Day Up > |
14.2. Reading User Input14.2.1 Variables (Review)In the last chapter we talked about declaring and unsetting variables. Variables are set local to the current shell or as environment variables. Unless your shell script will invoke another script, variables are normally set as local variables within a script. (See "Variables" on page 810.) To extract the value from a variable, precede the variable with a dollar sign. You can enclose the variable within double quotes and the dollar sign will be interpreted by the shell for variable expansion. Variable expansion is not performed if the variable is enclosed in single quotes. Example 14.3.1 name="John Doe" or declare name="John Doe" # local variable 2 export NAME="John Doe" # global variable 3 echo "$name" "$NAME" # extract the value 14.2.2 The read CommandThe read command is a built-in command used to read input from the terminal or from a file (see Table 14.1). The read command takes a line of input until a newline is reached. The newline at the end of a line will be translated into a null byte when read. If no names are supplied, the line read is assigned to the special built-in variable REPLY. You can also use the read command to cause a program to stop until the user hits Enter. To see how the read command is most effectively used for reading lines of input from a file, see "Looping Commands" on page 903. The –r option to read causes the backslash/newline pair to be ignored; the backslash is treated as part of the line. The read command has four options to control its behavior: –a, –e, –p, and –r.[2]
Example 14.4.(The Script) #!/bin/bash # Scriptname: nosy echo -e "Are you happy? \c" 1 read answer echo "$answer is the right response." echo -e "What is your full name? \c" 2 read first middle last echo "Hello $first" echo –n "Where do you work? " 3 read 4 echo I guess $REPLY keeps you busy! -----------------------------------------------------[a] 5 read -p "Enter your job title: " 6 echo "I thought you might be an $REPLY." 7 echo -n "Who are your best friends? " 8 read -a friends 9 echo "Say hi to ${friends[2]}." ------------------------------------------------------- (The Output) $ nosy Are you happy? Yes 1 Yes is the right response. 2 What is your full name? Jon Jake Jones Hello Jon 3 Where do you work? the Chico Nut Factory 4 I guess the Chico Nut Factory keeps you busy! 5 Enter your job title: Accountant 6 I thought you might be an Accountant. 7,8 Who are your best friends? Melvin Tim Ernesto 9 Say hi to Ernesto.
EXPLANATION
Example 14.5.(The Script) #!/bin/bash # Scriptname: printer_check # Script to clear a hung-up printer 1 if [ $LOGNAME != root ] then echo "Must have root privileges to run this program" exit 1 fi 2 cat << EOF Warning: All jobs in the printer queue will be removed. Please turn off the printer now. Press return when you are ready to continue. Otherwise press Control C. EOF 3 read JUNK # Wait until the user turns off the printer echo 4 /etc/rc.d/init.d/lpd stop # Stop the printer 5 echo -e "\nPlease turn the printer on now." 6 echo "Press Enter to continue" 7 read JUNK # Stall until the user turns the printer back on echo # A blank line is printed 8 /etc/rc.d/init.d/lpd start # Start the printer EXPLANATION
|
< Day Day Up > |