ヘルプのソースは何ですか?


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 14

回答:


19

help はbash組み込みであり、buildtimeからの他のbash組み込みの詳細のみを提供します。

のソースは、bashソースツリーの組み込みディレクトリにあるファイルhelpからコンパイル時に生成されますdef。あなたが見れば、ヘルプのソースコードcdその情報がその一部であることがわかります$SHORT_DOC。 情報にアクセスするためにhelp呼び出される配列shell_builtinsを使用します。


1
さらなる証拠:strings /bin/bash | grep 'Change the current directory to DIR'
200_成功2014

12

時々私はその情報が--helpによってそれを拡大しているようだと思う

help cdそして、cd --help根本的に異なっています。helpはシェルに組み込まれているコマンドであり、シェル組み込まれている他のコマンドに関する情報を提供します。つまり、これらは独自の実行可能ファイルではなく、などの機能ですbash。一部の組み込みコマンドにもスタンドアロンの実行可能バージョンがあるため、これは少し混乱する可能性があります。この場合、彼らは通常独自のマニュアルページを持っており、あなたが尋ねると実行可能パスが公開されますwhich [command]。manページまたはfromの情報[command] --helpは実行可能ファイルに関するものです。からの情報help [command]は組み込みのものですが、うまくいけばそれらはほぼ同じです。組み込みだけのコマンドのmanページを探すと、おそらくその組み込みコマンドのすべてをリストするシェルのページが表示されます。

--help(省略形を含む-h)は、実行可能ファイルのコマンドラインオプションの従来のラベルです。すべてではありませんが、多くのCLIツールがこれを実装していますが、それらはバインドされておらず、提供される情報は実装に完全に依存しています。--help組み込みのシェルを呼び出すと、「無効なオプション」と短い「使用法」メッセージが表示される可能性があります。あなたはそれを実装していないスタンドアロンでそれを呼び出す場合は、ありも「無効なオプション」を得るが、まさに再び起こることはアプリケーションに依存します。

コマンドの組み込みバージョンとスタンドアロンバージョンの両方が利用可能であり、コマンドを呼び出すときにどちらが使用されるかを知りたい場合はtype、別のシェル組み込みのを使用できます。

> 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ますが、シェルが呼び出すのは組み込みであることがわかります。


1
type2回入力する必要はありません:組み込み、シェル関数、エイリアスを含む、($ PATHで定義された)範囲内のtype -a echoすべての呼び出しを返しechoます。参照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内部コマンド)です。この第3部のスクリプトは、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バイナリ内で生成されます。


それらはハードコードされていませんが、ビルド時に生成されます
Ulrich Dangel 2014

ありがとう。(1)「組み込みヘルプを使用トレースと呼ぶ」とはどういう意味ですか?(2)組み込みコマンドの実行可能ファイルに使用法の情報がハードコーディングされていますcdか?
2014

@UlrichDangel-訂正ありがとうございます。ハードコードは、コンパイル時にバイナリ内ではなく、ソースコードで修正されたものに関連します。私の悪い;)@ティム。strace決定されたコマンドが実行中に何をしているかを確認するためのツールです(ライブラリ、システムコール、開かれたファイルなど)。このメソッドwriteは、ヘルプ情報が、組み込みコマンドhelpの使用中にバイナリ(bash)の内部から取得され、ファイル(manページなど)を開くことから取得されないことを示しています。

2

--helpは実行可能ファイルの一部であり、そこで実装する必要があると思います。これが、-helpの異なるバージョンが表示される理由です。時々-h省略形が許可されます。それ以外は、接頭辞のない「ヘルプ」です...

編集する

あなたの質問の一部を読み間違えました。「help」コマンド自体の内部動作に慣れていません。


コマンドはどのようにしてhelp別のコマンドの情報を見つけますか?
Tim
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.