我需要解释以下命令的语法:
while read -u3 -r f1
为什么-u,为什么-r?我知道3是文件描述符或标识符吗?
答案 0 :(得分:2)
来自info "(bash)Bash Builtins"
(web mirror):
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name …]
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:
[...]
-r
Raw mode: a `\' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.
[...]
-u fd
Read input from file descriptor fd.
总而言之,您的命令从文件描述符3读取,忽略行继续和转义的反斜杠。对于每一行,行的内容放在$f1
中,并调用while循环的一次迭代。
答案 1 :(得分:2)
它从文件描述符3读取而不将输入中的反斜杠视为转义字符。读取的每一行都被指定为名称f1
。
来自manual:
-r
如果给出此选项,则反斜杠不会充当转义字符。反斜杠被认为是该行的一部分。在 特别是,反斜杠 - 换行符对不能用作一行 延续。
-u fd
从文件描述符 fd 读取输入。
答案 2 :(得分:1)
首先,read
是内置的shell(try type read
)。这就是为什么你不会将它作为单个手册页找到。
但是,作为bash
内置版,您可以在bash
manpage
(或通过help read
,help
成为另一个bash
找到其文档内置{1}}。因此,引用联机帮助页的适当部分,这是:
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars]
[-p prompt] [-t timeout] [-u fd] [name ...]
One line is read from the standard input, or from the file descriptor fd
supplied as an argument to the -u option, and the first word is assigned
to the first name, the second word to the second name, and so on, with
leftover words and their intervening separators assigned to the last name.
If there are fewer words read from the input stream than names, the
remaining names are assigned empty values.
The characters in IFS are used to split the line into words. The backslash
character (\) may be used to remove any special meaning for the next
character read and for line continuation. Options, if supplied, have the
following meanings:
-a aname
The words are assigned to sequential indices of the array variable
aname, starting at 0. aname is unset before any new values are
assigned. Other name arguments are ignored.
-d delim
The first character of delim is used to terminate the input line,
rather than newline.
-e If the standard input is coming from a terminal, readline (see
READLINE above) is used to obtain the line. Readline uses the
current (or default, if line editing was not previously active)
editing settings.
-i text
If readline is being used to read the line, text is placed into
the editing buffer before editing begins.
-n nchars
read returns after reading nchars characters rather than waiting
for a complete line of input, but honor a delimiter if fewer than
nchars characters are read before the delimiter.
-N nchars
read returns after reading exactly nchars characters rather than
waiting for a complete line of input, unless EOF is encountered
or read times out. Delimiter characters encountered in the
input are not treated specially and do not cause read to return
until nchars characters
are read.
-p prompt
Display prompt on standard error, without a trailing newline,
before attempting to read any input. The prompt is displayed only
if input is coming from a terminal.
-r Backslash does not act as an escape character. The backslash is
considered to be part of the line. In particular, a
backslash-newline pair may not be used as a line continuation.
-s Silent mode. If input is coming from a terminal, characters are
not echoed.
-t timeout
Cause read to time out and return failure if a complete line of
input is not read within time‐out seconds. timeout may be a
decimal number with a fractional portion following the decimal
point. This option is only effective if read is reading input from
a terminal, pipe, or other special file; it has no effect when
reading from regular files. If timeout is 0, read returns success
if input is available on the specified file descriptor, failure
otherwise. The exit status is greater than 128 if the timeout is
exceeded.
-u fd Read input from file descriptor fd.
If no names are supplied, the line read is assigned to the variable REPLY.
The return code is zero, unless end-of-file is encountered, read times out
(in which case the return code is greater than 128), or an invalid file
descriptor is supplied as the argument to -u.
在您的情况下,让我们先看一下read -u3 -r f1
的选项和参数:
-u3
:从文件描述符3
读取输入(请记住0
为stdin
,1
为stdout
且{{1} } 2
。stderr
不是特别的东西,只是一个文件。)3
:反斜杠不作为转义字符。-r
:该行的目标变量是否从f1
读取。因此,只要您可以从fd 3
中提取一行,忽略反斜杠并复制fd 3
中的当前行,就意味着您正在循环读取。