帮助的来源是什么?


8

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`

3
只是为了澄清,cd没有--help选择。您所看到的是尝试使用无效选项标志时获得的基本用法消息。
terdon

另请参见man命令,以及在支持该info命令的系统中。
keshlam

Answers:


19

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

的源help是在编译时从defbash源树的内置目录中的文件生成的。如果你看一下帮助源代码cd你会发现,信息的一部分$SHORT_DOChelp使用称为shell_builtins访问信息的数组。


1
进一步的证据:strings /bin/bash | grep 'Change the current directory to DIR'
200_success 2014年

12

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

help cdcd --help根本不同。help是shell内置的命令,它提供了有关其他命令的信息内置到外壳,这意味着,他们没有自己的可执行文件,它们的特征,例如bash。由于某些内置命令还具有独立的可执行版本,因此这可能会造成一些混乱。在这种情况下,它们通常具有自己的手册页,并且在您询问时会公开可执行路径which [command]。手册页中的信息或来自[command] --help于可执行文件的信息;来自的信息help [command]是针对内置的,但希望它们或多或少是相同的。如果您为仅内置命令查找手册页,则可能会获得一个外壳程序页面,其中列出了所有内置命令。

--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调用是内置的。


1
无需键入type两次:type -a echo将所有调用返回到echo您可及的范围内(由$ PATH定义),包括内置函数,shell函数和别名。请参阅help type以供参考。
Tatjana Heuser

8

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

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.

帮助是一个BUILTIN命令(意味着,bash内部命令),用于获取其他内置命令的信息。由于该第三部分脚本不是bash的内置命令。如果您运行bash,将内置函数help称为用法,strace您将获得:

# 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二进制文件中的构建时生成的。


他们不是硬编码,但在编译时产生
乌尔里希Dangel

谢谢。(1)“将内置帮助称为使用跟踪”是什么意思?(2)使用信息硬编码在内置命令的可执行文件中cd吗?
蒂姆(Tim)

@UlrichDangel-感谢您的纠正。硬编码与固定在源代码上的东西有关,而不与编译期间的二进制文件内的东西有关。我不好;)@Tim。strace是一种工具,用于查看执行过程中确定的命令正在执行的操作(库,系统调用,打开的文件等)。该方法write显示,在使用help内置命令时,帮助信息来自二进制文件(bash)内部,而不是来自打开文件(如手册页)。

2

我相信--help是可执行文件的一部分,必须在此处执行。这就是为什么您看到不同版本的--help,有时允许-h简写,而其他版本则是非前缀的“ help”…

编辑

我误解了您的部分问题。我不熟悉“帮助”命令本身的任何内部工作方式。


该命令如何help查找另一个命令的信息?
蒂姆(Tim)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.