9.2. Reading User Input

9.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 in Chapter 8, "Variables" on page 327.)

To extract the value from a variable, preceed 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 9.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)
					

9.2.2. The read Command

The read command is a built-in command used to read input from the terminal or from a file (see Table 9.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 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 425. 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]

[2] Optioins -a, -e and -p are available only in bash versions 2.x.

Table 9.1. The read Command
FormatMeaning
read answerReads a line from standard input and assigns it to the variable answer.
read first lastReads a line from standard input to the first white space or newline, putting the first word typed into the variable first and the rest of the line into the variable last.
readReads a line from standard input and assigns it to the built-in variable, REPLY.
read -a arraynameReads a list of words into an array called arrayname.[a]
read -eUsed in interactive shells with command line editing in effect; e.g., if editor is vi, vi commands can be used on the input line.[a]
read -p promptPrints a prompt, waits for input, and stores input in REPLY variable.[a]
read -r lineAllows the input to contain a backslash.[a]

[a] Not implemented on versions of bash prior to 2.0.

Example 9.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.
					

aThe commands listed below this line are not implemented on versions of bash prior to 2.x.

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.

  3. A line is read from standard input and stored in the built-in REPLY variable.

  4. The value of the REPLY variable is printed.

  5. With the -p option, the read command produces a prompt, Enter your job title: and stores the line of input in the special built-in REPLY variable.

  6. The value of the REPLY variable is displayed in the string.

  7. The user is asked to enter input.

  8. With the -a option, the read command takes input as an array of words. The array is called friends. The elements read into the array are Melvin, Tim, and Ernesto.

  9. The third element of the friends array, Ernesto, is printed. Array indices start at 0.

Example 9.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 "
Please turn the printer on now."
6  echo "Press return 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

  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 JUNK 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 lpd program stops the printer daemon.

  5. Now it's time to turn the printer back on!

  6. The user is asked to press the carriage return when ready.

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

  8. The lpd program starts the print daemons.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.216.175