为什么 read 没有正常的手册页?

Why does read not have a normal man page?

我很想了解有关 'read' 的更多信息,以提高我的 shell 脚本编写能力。

通常当我对任何程序有求知欲时,我只需输入:

man [program]

然后我学习如何编写程序。在过去的 15 年里,我一直这样做。

但几天前,我无意中发现了一个名为 'read' 的程序的最无用和最意想不到的手册页 (IMO)。它是读取用户输入的常用程序,许多 shell 脚本都在使用它。

这是我的一个 shell 脚本中使用的。它只是获取一个变量并输出它,同时允许最终用户对其进行编辑:

read -e -p "HOSTip: " -i "${HOSTip}" HOSTip

这个命令花了很多时间在网上搜索以找到如何以这种方式使用它。手册页是零帮助。

如果能帮助我弄清楚为什么我的机器上似乎没有使用 read 的正确手册,我们将不胜感激。

如果您的 shell 是 bash,它的内置命令没有单独的手册页。使用 help 查看内置列表,使用 help <command> 获取有关单个内置列表的帮助。

$ help
GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

 job_spec [&]                                               history [-c] [-d offset] [n] or history -anrw [filename>
 (( expression ))                                           if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMA>
 . filename [arguments]                                     jobs [-lnprs] [jobspec ...] or jobs -x command [args]
 :                                                          kill [-s sigspec | -n signum | -sigspec] pid | jobspec >
 [ arg... ]                                                 let arg [arg ...]
 [[ expression ]]                                           local [option] name[=value] ...
 alias [-p] [name[=value] ... ]                             logout [n]
 bg [job_spec ...]                                          mapfile [-d delim] [-n count] [-O origin] [-s count] [->
 bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u>  popd [-n] [+N | -N]
 break [n]                                                  printf [-v var] format [arguments]
 builtin [shell-builtin [arg ...]]                          pushd [-n] [+N | -N | dir]
 caller [expr]                                              pwd [-LP]
 case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... es>  read [-ers] [-a array] [-d delim] [-i text] [-n nchars]>
 cd [-L|[-P [-e]] [-@]] [dir]                               readarray [-d delim] [-n count] [-O origin] [-s count] >
 command [-pVv] command [arg ...]                           readonly [-aAf] [name[=value] ...] or readonly -p
 compgen [-abcdefgjksuv] [-o option] [-A action] [-G glob>  return [n]
 complete [-abcdefgjksuv] [-pr] [-DEI] [-o option] [-A ac>  select NAME [in WORDS ... ;] do COMMANDS; done
 compopt [-o|+o option] [-DEI] [name ...]                   set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...>
 continue [n]                                               shift [n]
 coproc [NAME] command [redirections]                       shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFgiIlnrtux] [-p] [name[=value] ...]           source filename [arguments]
 dirs [-clpv] [+N] [-N]                                     suspend [-f]
 disown [-h] [-ar] [jobspec ... | pid ...]                  test [expr]
 echo [-neE] [arg ...]                                      time [-p] pipeline
 enable [-a] [-dnps] [-f filename] [name ...]               times
 eval [arg ...]                                             trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [argument ...]] [redirecti>  true
 exit [n]                                                   type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or export -p               typeset [-aAfFgiIlnrtux] [-p] name[=value] ...
 false                                                      ulimit [-SHabcdefiklmnpqrstuvxPT] [limit]
 fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [>  umask [-p] [-S] [mode]
 fg [job_spec]                                              unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMANDS; done               unset [-f] [-v] [-n] [name ...]
 for (( exp1; exp2; exp3 )); do COMMANDS; done              until COMMANDS; do COMMANDS; done
 function name { COMMANDS ; } or name () { COMMANDS ; }     variables - Names and meanings of some shell variables
 getopts optstring name [arg ...]                           wait [-fn] [-p var] [id ...]
 hash [-lr] [-p pathname] [-dt] [name ...]                  while COMMANDS; do COMMANDS; done
 help [-dms] [pattern ...]                                  { COMMANDS ; }
$ help read
read: read [-ers] [-a array] [-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:
      -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
      -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.

您还可以在 man builtins 或靠近 bash man page 底部的“Shell 内置命令”下找到他们的文档。