0

我需要解释以下命令的语法:

 while read -u3 -r f1 

为什么是 -u ,为什么是 -r ?我知道 3 是文件描述符或标识符,对吗?

4

3 回答 3

2

它从文件描述符 3 中读取,而不将输入中的反斜杠视为转义字符。读取的每一行都分配了名称f1

手册

-r

如果给出此选项,则反斜杠不会充当转义字符。反斜杠被认为是该行的一部分。特别是,反斜杠换行对不能用作续行。

-u fd

从文件描述符fd读取输入。

于 2013-08-22T08:04:35.293 回答
2

来自info "(bash)Bash Builtins"网络镜像):

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循环的一次迭代。

于 2013-08-22T08:03:16.930 回答
1

首先,read是一个内置的shell(try type read)。这就是为什么您不会将其作为单个手册页找到的原因。

但是,作为一个bash内置的,您可以在bash manpage(或通过help readhelp作为另一个bash内置)找到它的文档。因此,引用手册页的适当部分,这里是:

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(记住0is stdin, 1isstdout2is stderr.3不是什么特别的东西,只是一个文件)。
  • -r: 反斜杠不充当转义字符。
  • f1: 是从 读取的行的目标变量fd 3

因此,这意味着只要您可以从中提取一行,您就在循环读取fd 3,忽略反斜杠并将当前行复制到f1.

于 2013-08-22T08:19:42.800 回答