< Day Day Up > |
8.2. Reading User InputThe read command is a built-in command used to read input from the terminal or from a file (see Table 8.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. You can also use the read command to cause a program to stop until the user enters a carriage return. To see how the read command is most effectively used for reading lines of input from a file, see "Looping Commands" on page 349. Example 8.3.(The Script) #!/bin/sh # Scriptname: nosy echo "Are you happy? \c" 1 read answer echo "$answer is the right response." echo "What is your full name? \c" 2 read first middle last echo "Hello $first" ------------------------------------------------------------ (The Output) Are you happy? Yes 1 Yes is the right response. 2 What is your full name? Jon Jake Jones Hello Jon Table 8.1. The read Command
EXPLANATION
Example 8.4.(The Script) #!/bin/sh # Scriptname: printer_check # Script to clear a hung up printer for SVR4 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 Enter when you are ready to continue. Otherwise press Ctrl-C. EOF 3 read ANYTHING # Wait until the user turns off the printer echo 4 /etc/init.d/lp stop # Stop the printer 5 rm -f /var/spool/lp/SCHEDLOCK /var/spool/lp/temp* echo echo "Please turn the printer on now." 6 echo "Press Enter to continue" 7 read ANYTHING # Stall until the user turns the printer back on echo # A blank line is printed 8 /etc/init.d/lp start # Start the printer EXPLANATION
|
< Day Day Up > |