read:
Brief introduction:
The read command reads the value of a variable from the keyboard and is typically used in a shell script to interact with the user. The command can read the values of multiple variables at once, and the variables and input values need to be separated by spaces. After the read command, if no variable name is specified, the read data is automatically assigned to the specific variable reply.
Grammar
Read (options) (parameter
Options
Read can be available with-a,-D,-E,-N,-p,-r,-T, and-s eight options.
-A: Read content into numeric values
Echo-n "Input muliple values into an array:"
Read-a Array
-D: Represents the delimiter, the delimiter, which is normally the interval of the IFS parameter, but through-D, we can define the character position that reads until execution occurs. For example Read–d Madfds value, when reading a character with M, does not continue to read backwards, for example, the input is Hello m, the valid value is "Hello", please note that the space in front of the m will be deleted. You can enter multiple strings in this way, such as define "." As a knot symbol and so on.
-e: Only for scripts that interact with each other, and it uses ReadLine to collect input rows. Read these few words don't understand what meaning, first skip.
-N: Used to limit the maximum number of characters that can be read in as valid. For example Echo–n 4 value1 value2, if we try to enter 12 34, then only the previous valid 12 3, as input, in fact, after you enter the 4th character ' 3 ', automatically end the input. The result here is value 12,value2 of 3.
-P: For the prompt, in the previous example we used the Echo–n "..." to give a prompt, you can use the Read–p ' ... my promt? ' The way value is represented by just one statement.
-r: In the parameter input, we can use the '/' to indicate that there is no input, newline continues to enter, if we need to line the last '/' as a valid character, can be done through-R. Also in the input characters, we want special characters such as/n to take effect, and the-r option should also be used.
-S: For some special symbols, such as arrows, do not print them on the terminal, such as Read–s key, we press the cursor, after the carriage return, if we ask to display, that is, ECHO, the cursor upward, if not using-s, at the input, the input display ^[[a, That is, print on the terminal, and then if ECHO is required, the cursor will move up.
-T: Used to indicate the time to wait for input, in seconds, to wait longer, to continue execution of subsequent scripts, and note that the parameter retains its original value as null input.
Parameters
Variable: Specifies the variable name of the Read value.
Example 1:
Read input from standard input and assign value to name
# Read Name
Sxzhou
Print variable name
# echo $name
Sxzhou
Example 2:
-P Specify prompt
# read-p "Input your name:" Name
Input your Name:sxzhou
# echo $name
Sxzhou
Example 3:
Specifying multiple variables
# read-p "Input animal name:" Cat Dog
Input Animal Name:tom Flower
# echo $cat
Tom
# echo $dog
Flower
Example 4:
If a variable is not specified in the Read command, then the read name places any data it receives in the special environment variable reply
# Read
This is reply
# echo $REPLY
This is reply
Example 5:
Using reply to find factorial
# VI product.sh
#!/bin/bash
Read-p "Enter a number:"
Product=1
for ((count=1; count<= $REPLY; count++))
Do
product=$[$product * $count]
Done
echo "The product of $REPLY is $product"
# sh product.sh
4
The product of 4 is 24
Attention:
There is a space between the parenthesis and the bracket symbol and the content
No spaces on both sides of the equals sign
Example 6:
Timeout Test read-t
# VI overtime.sh
#!/bin/bash
If read-t 5-p "Please enter your name:" Name
Then
echo "Hello $name, welcome!"
Else
Echo
echo "Sorry, too slow! Please retry "
Fi
Enter a name in five seconds
# sh overtime.sh
Please enter your name:
Sxzhou
Hello Sxzhou, welcome!
Five seconds away
# sh overtime.sh
Please enter your name:
Sorry, too slow! Please retry
Example 7:
The read command determines the input
# VI decide.sh
#!/bin/bash
Read-n1-p "Do you want to continue [Y (y)/n (n.)]?" Answer
Case $answer in
Y | Y) echo
echo "Fine, continue ...";;
N | N) Echo
echo "Ok,goodbay"
exit;;
Esac
# sh decide.sh
Do you want to continue [Y (y)/n (n.)]? Y
Fine, continue ...
# sh decide.sh
Do you want to continue [Y (y)/n (n.)]? N
Ok,goodbay
Note:
-n1 Enter a character for the limit
Example 8:
Hide Input Read-s
# VI hide.sh
#!/bin/bash
Read-s-P "Enter your passwd:" Pass
Echo
echo "Is your passwd readlly $pass?"
# sh hide.sh
Enter your passwd: (input 123456, not shown)
Is your passwd readlly 123456?
Example 9:
# VI text.sh
#!/bin/bash
Count=1
Cat hide.sh | While Read line
Do
echo "line $count: $line"
count=$[$count + 1]
Done
echo "Finished processing the file"
# sh text.sh
Line 1: #!/bin/bash
Line 2:read-s-P "Enter your passwd:" Pass
Line 3:echo
Line 4:echo "is your passwd readlly $pass?"
Read of the shell command