14.2 Read user input
14.2.1 variable
In the previous chapter we talked about how to define or cancel a variable, which can be set to the current shell's local variable, or to an environment variable. 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, the dollar character is followed by the variable name. The shell performs a variable extension of the variable in the dollar symbol in double quotes, and the dollar symbol in single quotation marks is not extended by the execution of the 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 a line are converted to a null character when they are read. If the read command does not follow the variable name, the reading line is assigned to the built-in variable reply. You can also use the Read command to interrupt the operation of the program until the user enters a enter key. To know how to efficiently read input lines from a file using the Read command, see section 14.6, "Circular control commands". With the-r option, the read command ignores the backslash/newline pair and the backslash as part of the line. The read command has 4 control options:-a,-e,-p,-r②.
Table 14-1 Read command
Grid & nbsp; |
contains semantics |
Read an Swer |
Reads a row from standard input and assigns a variable answer |
Read First last< /p> |
reads a line from standard input until the first space or line break is encountered. Save the first word that the user typed into the variable number, saving the remainder of the row to the last variable |
Read |
< p> standard input reads a row and assigns a value to the built-in variable reply |
Read–a arrayname |
Read into a set of words, assigned to the array Arrayname③ |
(Continuation of table)
Format |
Meaning |
Read-e |
Enable the editor in 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 the prompt, wait for input, and assign the input to the reply variable ③ |
Read–r Line |
Allow input to include a backslash ③ |
Example 14-4
Script
#!/bin/bash
# Scriptname:nosy
Echo-e "Is You happy?" \c "
1 Read Answer
echo "$answer is the right response."
Echo-e "What's 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!
-------------------------------------------------------④
5 read-p "Enter Your job title:"
6 echo "I thought you might is an $REPLY."
7 Echo-n "Who is your best friends?"
8 Read-a Friends
9 echo "Say hi to ${friends[2]}."
-------------------------------------------------------
Output
$ nosy
Is you happy? Yes
1 Yes the right response.
2 What's your full name? Jon Jake Jones
Hello Jon
3 Where do I 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 is 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 input exposition to the variable, exposition the second to the variable middle, and assigns all remaining words until the end of the line to the variable last.
3. Read a line 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 displays the prompt "Enter your job title:" To assign the input line to a 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 sets the input as an array of words, the array name is 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.
Shell reads user input