SHELL script -- read command, shell script -- read
To interact with Linux, it is essential for the script to obtain the keyboard input results. read can read the keyboard input characters.
read [-rs] [-a ARRAY] [-d delim] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [var_name1 var_name2 ...]
The read command reads a single row from the standard input, splits the read row into multiple fields based on the IFS Variable, and assigns the split fields to the specified var_name Variable list. The first field is allocated to the first variable var_name1, and the second field is allocated to the second variable var_name2, which ends at the end. If the specified variable name is less than the number of fields, the number of extra fields is also allocated to the last var_name. If the specified variable command is more than the number of fields, the value of the extra variable is null.
If no var_name is specified, all the split fields are stored in the specific variable REPLY.
Option Description:-a: stores the split fields in the specified array sequentially. the starting position of the storage starts from index = 0 of the array. -D: Specifies the end symbol of the read row. The end symbol is a line break by default. -N: The read will automatically end when N characters are read. If N characters are not fully read, press ENTER or encounter a line break, the read will also end. -N: It is strictly required that the read process be automatically completed after N characters are fully read, even if you press ENTER or encounter a linefeed. The line break or press enter is a character. -P: give a prompt. For example, "-p, enter the password:"-r: Disable the escape Function of the backslash. This means that "\" will become part of the text. -S: silent mode. The input content is not displayed on the screen. -T: time-out time. When the time-out time is reached, read exits and an error is returned. That is to say, no content is read, even if a part has been input.
Example:
(1). Allocate the read content to the array variable and start from index 0.
[root@xuexi ~]# read -a array_testwhat is you name? [root@xuexi ~]# echo ${array_test[@]}what is you name?[root@xuexi ~]# echo ${array_test[0]}what
(2). Specify the end symbol of the row to be read, instead of the line break.
[Root @ xuexi ~] # Read-d '/' what is your name \ // # enter the "/" at the end to automatically end the read
Because var_name is not specified, you can use the $ REPLY variable to view the rows read by read.
[root@xuexi ~]# echo $REPLYwhat is you name /
(3). Restrict the input characters.
For example, if you enter 5 characters, it will end.
[Root @ xuexi tmp] # read-n 512345 [root @ xuexi tmp] # echo $ REPLY # Enter 12345 for a total of 5 Characters and 12345 characters
If the number of characters entered is less than 5, press enter to immediately stop reading.
[root@xuexi ~]# read -n 5123[root@xuexi ~]# echo $REPLY123
However, if "-N 5" is used instead of "-n 5", the read will only end after the full five characters are read.
[Root @ xuexi ~] # Read-N 5123 \ n4 [root @ xuexi ~] # Read-N 5123 # The carriage return (line feed) after 3 is a character 4
(4) use the-p option to give the input prompt.
[root@xuexi ~]# read -p "pls enter you name: "pls enter you name: Junmajinlong[root@xuexi ~]# echo $REPLYJunmajinlong
(5). The escape Function of the backslash is prohibited.
[root@xuexi ~]# read -rwhat is you name \?[root@xuexi ~]# echo $REPLYwhat is you name \?
(6). Do not display the entered characters. For example, when you enter the password, do not explicitly enter the password.
[root@xuexi ~]# read -s -p "please enter your password: "please enter your password:[root@xuexi ~]# echo $REPLY123456
(7) split the read rows and assign values to the variables.
[root@xuexi ~]# read var1 var2 var3abc def galsl djks[root@xuexi ~]# echo $var1:::$var2:::$var3abc:::def:::galsl djks
(8). The input time limit is given. Incomplete input is discarded, so the variable value is null (if the variable has been assigned a value before the read operation, the variable will be overwritten as null after the read timeout ).
[root@xuexi ~]# var=5[root@xuexi ~]# read -t 3 var1[root@xuexi ~]# echo $var
Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Reprinted please indicate the source: Success!