The usage of read in the shell is detailed
Original: http://blog.csdn.net/jerry_1126/article/details/77406500
The common usage of read is as follows:
Read-[pstnd] var1 var2 ...
-P Prompt Statement
-N Character Count
-S shielded Echo
-T wait time
-D Input Demarcation
[Plain]View PlainCopy
- Read # reads a line from standard input and assigns a value to a specific variable reply
- [Email protected]~# Read
- hello,world!
- [Email protected]~# Echo $REPLY
- hello,world!
- Read name # reads input from standard input and assigns a value to the variable name
- [Email protected]~# Read name
- Jerry
- [Email protected]~# Echo $name
- Jerry
- Read Var1 var2 # The first variable is placed in Var1, and the second variable is placed in the VAR2
- [Email protected]~# read FirstName LastName
- Jerry Gao
- [Email protected]~# echo "FirstName: $firstname LastName: $lastname"
- Firstname:jerry Lastname:gao
- ). Read-p "Text" # print hint ' text ', wait for input, and store input in reply
- [Email protected]~# read-p ' please Enter your name:--> '
- Please Enter your Name:-->jerry
- [Email protected]~# Echo $REPLY
- Jerry
- Read-p "text" var # print hint ' text ', wait for input, and store input in Var
- [Email protected]~# read-p ' please Enter your name:--> ' name
- Please Enter your Name:-->jerry
- [Email protected]~# Echo $name
- Jerry
- Read-p "Text" var1 var2 # Print hint ' text ', wait for input, store variables in var1,var2 ...
- [Email protected]~# read-p ' What your name? ' FirstName LastName
- What your name? Jerry Gao
- [Email protected]~# echo "Firstname: $firstname Lastname: $lastname"
- Firstname:jerry Lastname:gao
- ). Read-r line # allow input with backslash
- [Email protected]~# read Line # without the-r parameter; backslashes are not displayed
- This was line 1. \ This was line 2.
- [Email protected]~# echo $line
- This was line 1. This was line 2.
- [Email protected]~# read-r Line # with-r parameter; The backslash is displayed normally
- This was line 1. \ This was line 2.
- [Email protected]~# echo $line
- This was line 1. \ This was line 2.
- ). Read-t 5 # Specifies a read wait time of 5 seconds
- [Email protected]~# read-t 5-p ' Your name: ' Name
- Your Name:jerry
- [Email protected]~# echo $name # cannot be entered if 5 seconds have not been entered
- Jerry
- Read-a Arrayname # Read the list of words into the arrayname array
- [Email protected]~# read-a citys
- BJ SH CD GZ
- [Email protected]~# echo ${citys[*]}
- BJ SH CD GZ
- [Email protected]~# echo ${citys[0]}
- BJ
- Read-s-P "pwd:" PWD # using the-s parameter can not display the user's input
- [Email protected]~# read-p "Enter Your Password:"-S Password
- Enter Your Password:
- [Email protected]~#
- [Email protected]~# echo $PASSWORD # The password you just entered is: 1234
- 1234
- one). Read-n 1-p "sure? (y/n): "# Use-N to determine the number of arguments
- [Email protected]~# read-n 1-p "is you sure? (y/n): "ANSWER
- Is you sure? (y/n): Y
- [Email protected]~#
- [Email protected]~# echo-e "Your ANSWER is: $ANSWER"
- Your ANSWER Is:y
- ). Read-d ":" var # using: As input delimiter
- [Email protected]~# read-d ";"-P "Enter Your name:" Name
- Enter Your Name:jerry Gao;
- [Email protected]~# echo-e "Your Name: $name"
- Your Name:jerry Gao
Read in the script application:
Traversal mode one:
[Python]View PlainCopy
- #!/bin/bash
- count=0
- While Read line
- Do
- Echo-e "$count:--> $line"
- count=$[count + 1]
- Done </etc/passwd
Traverse mode Two:
[Python]View PlainCopy
- #!/bin/bash
- Awk-f: ' {print $1,$7} '/etc/passwd | While read user bash
- Do
- Echo-e "user= $user; Bash= $bash "
- Done
Go The usage of read in the shell is detailed