read: read [-ers] [-a 数组] [-d 分隔符] [-i 缓冲区文字] [-n 读取字符数] [-N 读取字符数] [-p 提示符] [-t 超时] [-u 文件描述符] [名称 ...]
Read a line from the standard input and split it into fields.
Reads a single line from the standard input, or from file descriptor FD
if the -u option is supplied. The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME. Only the characters found in $IFS are recognized as word
delimiters.
If no NAMEs are supplied, the line read is stored in the REPLY variable.
Options:
-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero
-d delim continue until the first character of DELIM is read, rather
than newline
-e use Readline to obtain the line in an interactive shell
-i text Use TEXT as the initial text for Readline
-n nchars return after reading NCHARS characters rather than waiting
for a newline, but honor a delimiter if fewer than NCHARS
characters are read before the delimiter
-N nchars return only after reading exactly NCHARS characters, unless
EOF is encountered or read times out, ignoring any delimiter
-p prompt output the string PROMPT without a trailing newline before
attempting to read
-r do not allow backslashes to escape any characters
-s do not echo input coming from a terminal
-t timeout time out and return failure if a complete line of input is
not read within TIMEOUT seconds. The value of the TMOUT
variable is the default timeout. TIMEOUT may be a
fractional number. If TIMEOUT is 0, read returns immediately,
without trying to read any data, returning success only if
input is available on the specified file descriptor. The
exit status is greater than 128 if the timeout is exceeded
-u fd read from file descriptor FD instead of the standard input
Exit Status:
The return code is zero, unless end-of-file is encountered, read times out
(in which case it's greater than 128), a variable assignment error occurs,
or an invalid file descriptor is supplied as the argument to -u.
read 从标准输入中读取数据到变量中:
root@kali:~# read abc
123
root@kali:~# echo $abc
123
root@kali:~# read abc def
321 654
root@kali:~# echo $abc $def
321 654
选项:
-a 可以把输入的多个值(用空格隔开),放入数组中:下面是个例子. test.sh脚本内容:
#!/bin/bash
echo "Input mutiple values into an array:"
read -a array
echo "get ${#array[@]} values in array"
echo "They are ${array[*]}."
执行结果如下:
root@kali:~# bash test.sh
Input mutiple values into an array:
1 2 3 4 5
get 5 values in array
They are 1 2 3 4 5.
关于数组的详细介绍可以看5.linux编程中脚本编程的数组部分,这里就不赘述了。
-i 使用 TEXT 文本作为 readline 的初始文字
-d :表示delimiter,即定界符,一般情况下是以IFS为参数的间隔,但是通过-d,我们可以定义一直读到出现执行的字符位置。例如read –d madfds value,读到有m的字符的时候就不在继续向后读,例如输入为 hello m,有效值为“hello”,请注意m前面的空格等会被删除。这种方式可以输入多个字符串,例如定义“.”作为结符号等等。
-e :只用于互相交互的脚本,它将readline用于收集输入行。读到这几句话不太明白什么意思,先跳过。
-n :用于限定最多可以有多少字符可以作为有效读入。例如echo –n 4 value1 value2,如果我们试图输入12 34,则只有前面有效的12 3,作为输入,实际上在你输入第4个字符‘3’后,就自动结束输入。这里结果是value为12,value2为3。
-N nchars在准确读取了 nchars 个字符之后返回,除非遇到了文件结束符或者读超时,任何的分隔符都被忽略
-p :用于给出提示符,在前面的例子中我们使用了echo –n “…“来给出提示符,可以使用read –p ‘… my promt?’value的方式只需一个语句来表示。
-r :在参数输入中,我们可以使用’\/’表示没有输入完,换行继续输入,如果我们需要行最后的’\/’作为有效的字符,可以通过-r来进行。此外在输入字符中,我们希望\/n这类特殊字符生效,也应采用-r选项。
-s :对于一些特殊的符号,例如箭头号,不将他们在terminal上打印,例如read –s key,我们按光标,在回车之后,如果我们要求显示,即echo,光标向上,如果不使用-s,在输入的时候,输入处显示^[[A,即在terminal上 打印,之后如果要求echo,光标会上移。
-t :用于表示等待输入的时间,单位为秒,等待时间超过,将继续执行后面的脚本,注意不作为null输入,参数将保留原有的值
-u : 从文件中读入而不是从标准输入中读。下面从网络中找到了一个例子: 创建test.sh脚本如下:
while read -u3 i && read -u4 j;do
echo $i $j
done 3<afile 4<bfile
afile写入
1
2
3
4
5
bfile写入
q
w
e
r
t
运行test.sh有如下的效果:
root@kali:~# bash test.sh
1 q
2 w
3 e
4 r
5 t
这个脚本的作用就是从afile和bfile中不断的读取值,放入i,j变量中,并一一打印出来。