14.2 reading user input
14.2.1 Variable
In the previous chapter we talked about how to define or cancel variables, which can be set to the current shell's local variables, or environment variables. If your shell script does not need to invoke other scripts, the variables are usually set to local variables within the script (see section 13.10, "variables").
To get the value of a variable, a dollar character followed by a variable name. The shell performs variable extensions on the variable after the dollar character within the double quotation marks, and the dollar character in the single quotation mark is not extended by the execution variable.
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 read command
The read command is a built-in command for reading input from a terminal or file (see table 14-1). The read command reads an input line until a newline character is encountered. NewLine characters at the end of the line are converted to a null character when they are read. If the read command is not followed by the variable name, the rows that are entered are assigned to the built-in variable reply. You can also use the Read command to interrupt the program until the user enters a enter key. To know how to effectively read input rows from a file using the Read command, see "Circular control Commands" in section 14.6. If you take the-r option, the read command ignores the backslash/newline pair and the backslash as part of the row. The read command has 4 control options:-a,-e,-p,-r②.
Table 14-1 Read command
Format |
Meaning |
Read answer |
Reads a row from the standard input and assigns it to the variable answer |
Read the Last |
Reads a row from standard input until the first blank or newline character is encountered. Save the first word typed by the user to the variable, the remainder of the line in the variable last |
Read |
Standard input reads a row and assigns values to built-in variables reply |
Read–a Arrayname |
Read a set of words and assign them to the array Arrayname③ |
(continued)
Format |
Meaning |
Read-e |
Enables the editor on the Interactive shell command line. For example, if the editor is VI, you can use the VI command when entering rows ③ |
Read–p prompt |
Print a prompt, wait for input, and assign input to reply variable ③ |
Read–r Line |
Allow input containing backslash ③ |
Example 14-4
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 a middle last
echo "Hello $first"
Echo–n "Where Do you work?"
3 Read
4 echo I guess $REPLY keeps you busy!
-------------------------------------------------------④
5 read-p "Enter Your job title:"
6 echo "I thought you might is an $REPLY."
7 Echo-n "Who are your best friends?"
8 Read-a Friends
9 echo "Say hi to ${friends[2]}."
-------------------------------------------------------
Output
$ nosy
Are you happy? Yes
1 Yes 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 is an accountant.
7,8 who are your best friends? Melvin Tim Ernesto
9 Say hi to Ernesto.
Description
1. The read command receives a line of user input and assigns its value to the variable answer.
2. The read command receives input from the user, assigns the first cifu of the input to the variable, the second Cifu to the variable middle, and assigns all remaining words until the end of the line to the variable last.
3. Read a row from the standard input and assign the value to the built-in variable reply.
4. Displays the value of the variable reply.
5. The read command with the-P option, which displays the prompt "Enter your job title:" Assigns the input row to the specific built-in variable reply.
6. Displays the value of the variable reply in the string.
7. Request user input.
8. The read command with the-a option consists of an array of input as a set of words, with an array named friends, and the elements that are read into the array are Melvin, Tim, and Ernesto.
9. Displays the 3rd element of the Friends array. The array subscript starts at 0.