编辑

编辑

help 显示有关内置命令的信息。帮助的来源是什么?它是否维护内置命令的数据库,或者是否读取每个内置命令的一些文件(类似于每个实用程序的联机帮助页)?

有时我发现它的信息似乎扩展了这一点--help

$ cd --help
bash: cd: --: invalid option
cd: usage: cd [-L|[-P [-e]]] [dir]

$ help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.

    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.

    Options:
        -L  force symbolic links to be followed
        -P  use the physical directory structure without following symbolic
        links
        -e  if the -P option is supplied, and the current working directory
        cannot be determined successfully, exit with a non-zero status

    The default is to follow symbolic links, as if `-L' were specified.

    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

我想help提取可执行文件的帮助部分,但是对于 python 脚本pdf-merge.py,这不是

$ help ./pdf-merge.py
bash: help: no help topics match `./pdf-merge.py'.  Try `help help' or `man -k ./pdf-merge.py' or `info ./pdf-merge.py'.

$ ./pdf-merge.py --help
usage: pdf-merge.py [-h] [-v] [--ask] [--output OUTPUT] [--title TITLE]
                    [--author AUTHOR] [--keyword KEYWORD] [--pdftk PDFTK]
                    [--gs GS] [--pdfmarks PDFMARKS] [--unicode]
                    PDF [PDF ...]

Merge PDFs preserving bookmarks. Thanks to Larry Cai for suggesting that
Unicode be supported and for discussion about the `--pdfmarks` option.

positional arguments:
  PDF                  an input PDF to merge

optional arguments:
  -h, --help           show this help message and exit
  -v, --version        show program's version number and exit
  --ask                pause for manual pdfmark tweaking
  --output OUTPUT      name of the output PDF
  --title TITLE        title of output PDF
  --author AUTHOR      author of output PDF
  --keyword KEYWORD    keywords for the output PDF
  --pdftk PDFTK        path to the pdftk executable
  --gs GS              path to the gs (Ghostscript) executable
  --pdfmarks PDFMARKS  path to pdfmarks file. If not given, a temporary file
                       is used. If given and the file is missing, execution
                       will stop after the file is created (before the
                       Ghostscript run). If given and the file exists, no
                       attempt will be make to use pdftk to generate the mark
                       file (I assume your input file is what you want).
  --unicode            instead of merging PDFs, convert PDF-formatted unicode
                       strings. For example `--unicode '<FEFF03B103B203B3>'
                       \u03b1\u03b2\u03b3`

答案1

help是一个 bash 内置函数,它只为您提供构建时其他 bash 内置函数的详细信息。

的源代码help是在编译时生成的defbash 源代码树的内置目录中的文件生成的。如果你看一下帮助源代码cd您会注意到该信息是 的一部分$SHORT_DOChelp使用名为 的数组shell_builtins来访问信息。

答案2

有时我发现它的信息似乎通过 --help 扩展了这一点

help cd并且cd --help有根本的不同。help是内置于 shell 中的命令,它提供有关其他命令的信息内置于外壳中,这意味着它们不是自己的可执行文件,它们是例如 的功能bash。这可能会有点令人困惑,因为一些内置命令也有独立的可执行版本。在这种情况下,它们通常有自己的手册页,并且如果您询问,它们会公开可执行路径which [command]。手册页中的信息或来自[command] --help可执行文件的信息;help [command]来自内置的信息,但希望它们或多或少相同。如果您查找仅内置命令的手册页,您可能会得到一个列出所有内置命令的 shell 页面。

--help(包括缩写形式-h)只是可执行文件的命令行选项的常规标签。许多(但不是全部)CLI 工具都实现了这一点,但它们不受约束,并且提供的信息完全取决于实现。如果您--help在内置 shell 上调用,您可能会收到“无效选项”和简短的“用法”消息。如果您在未实现它的独立系统上调用它,那么您可能也会得到一个“无效选项”,但到底会发生什么取决于应用程序。

如果某个命令有内置版本和独立版本可用,并且您想知道调用该命令时使用哪一个版本,则可以使用type另一个内置 shell。

> help type
type: type [-afptP] name [name ...]
Display information about command type.

For each NAME, indicate how it would be interpreted if used as a
command name.
[...]

> which echo
/bin/echo

> type echo
echo is a shell builtin

在这里我们可以看到,虽然有一个独立的可执行文件echo,但echo您的 shell 调用的是内置的。

答案3

你已经回答了你自己的问题:

nicolas@host:~$ help help
help: help [-s] [pattern ...]
    Display helpful information about builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise a list of the builtins is printed.  The -s option
    restricts the output for each builtin command matching PATTERN to
    a short usage synopsis.

Help 是一个 BUILTIN 命令(即 bash 内部命令),用于获取其他内置命令的信息。因为第三部分脚本不是 bash 的内置命令。如果你运行bash,调用内置的helpusestrace你将得到:

# strace bash -i -c "help cd"
---snip(long output)---
write(1, "cd: cd [-L|-P] [dir]\n"..., 21cd: cd [-L|-P] [dir]
) = 21
open("/usr/share/locale/locale.alias", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2570, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f38b765c000
read(3, "# Locale name alias data base.\n# "..., 4096) = 2570
read(3, ""..., 4096)                    = 0
close(3)                                = 0
munmap(0x7f38b765c000, 4096)            = 0
open("/usr/share/locale/pt_BR/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/pt/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/bash.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(1, "    Change the current directory "..., 710    Change the current directory to DIR.  The variable $HOME is the
    default DIR.  The variable CDPATH defines the search path for
    the directory containing DIR.  Alternative directory names in CDPATH
    are separated by a colon (:).  A null directory name is the same as
    the current directory, i.e. `.'.  If DIR begins with a slash (/),
    then CDPATH is not used.  If the directory is not found, and the
    shell option `cdable_vars' is set, then try the word as a variable
    name.  If that variable has a value, then cd to the value of that
    variable.  The -P option says to use the physical directory structure
    instead of following symbolic links; the -L option forces symbolic links
) = 710
write(1, "    to be followed.\n"..., 20    to be followed.
) = 20
---snip(long output)---

几乎意味着这些信息是在构建时在 bash 二进制文件中生成的。

答案4

我相信 --help 是可执行文件的一部分,它必须在那里实现。这就是为什么你会看到不同版本的 --help,有时允许使用 -h 简写,其他则是无前缀的“help”......

编辑

我误读了你问题的一部分。我不熟悉“help”命令本身的任何内部工作原理。

相关内容