8.2. Reading User Input

 <  Day Day Up  >  

8.2. Reading User Input

The 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

Format

Meaning

read answer

Reads a line from standard input and assigns it to the variable answer .

read first last

Reads a line from standard input to the first whitespace or newline, putting the first word typed into the variable first and the rest of the line into the variable last .


EXPLANATION

  1. The read command accepts a line of user input and assigns the input to the variable answer .

  2. The read command accepts input from the user and assigns the first word of input to the variable first , the second word of input to the variable middle , and all the rest of the words up to the end of the line to the variable last .

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

  1. Checks to see if user is root . If not, sends an error and exits.

  2. Creates a here document . Warning message is displayed on the screen.

  3. The read command waits for user input. When the user presses Enter, the variable ANYTHING accepts whatever is typed. The variable is not used for anything. The read in this case is used to wait until the user turns off the printer, comes back, and presses Enter.

  4. The lp program stops the printer daemon.

  5. The SCHEDLOCK file must be removed before the scheduler can start again, as well as temporary files in /var/spool/lp .

  6. The user is asked to press Enter when ready.

  7. Whatever the user types is read into the variable ANYTHING , and when Enter is pressed, the program will resume execution.

  8. The lp program starts the print daemons.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net