Qual è la fonte di aiuto?


8

help visualizza informazioni sui comandi integrati. Qual è la fonte di aiuto? Mantiene un database per i comandi integrati o legge alcuni file di ciascun comando incorporato (simile alla manpage di ciascuna utility)?

A volte trovo che le sue informazioni sembrino ampliarle di --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.

Ho pensato di helpestrarre la parte di aiuto dell'eseguibile, ma per uno script Python pdf-merge.py , non lo è

$ 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
Giusto per chiarire, cdnon ha --helpun'opzione. Quello che vedi è il messaggio di utilizzo di base che ricevi quando tenti di usare un flag di opzione non valido.
terdon

Vedi anche il mancomando e nei sistemi che lo supportano il infocomando.
Keshlam,

Risposte:


19

help è un built-in bash e ti fornisce solo i dettagli di altri built-in bash di buildtime.

Il sorgente per helpviene generato in fase di compilazione dai deffile nelle directory incorporate dell'albero dei sorgenti di bash. Se guardi il codice sorgente di aiuto e cdnoterai che le informazioni sono parte di $SHORT_DOC. helputilizza un array chiamato shell_builtinsper accedere alle informazioni.


1
Ulteriori prove:strings /bin/bash | grep 'Change the current directory to DIR'
200_successo

12

A volte trovo che le sue informazioni sembrino ampliare quelle di --help

help cde cd --helpsono fondamentalmente diversi. helpè un comando incorporato nella shell e fornisce informazioni su altri comandi che sono integrati nella shell , il che significa che non sono eseguibili propri, sono caratteristiche di, ad es bash. Questo può creare un po 'di confusione poiché alcuni comandi integrati hanno anche versioni eseguibili autonome. In questo caso, di solito hanno una propria pagina di manuale e, se richiesto, esporranno un percorso eseguibile which [command]. Le informazioni nella pagina man o da [command] --helpsono per l'eseguibile; le informazioni da help [command]sono per il built-in, ma speriamo che siano più o meno le stesse. Se cerchi una pagina man per un comando che è solo un built-in, probabilmente otterrai una pagina per la shell che elenca tutti i suoi comandi integrati.

--help(inclusa la forma abbreviata -h) è solo un'etichetta convenzionale per un'opzione della riga di comando di un eseguibile. Molti, ma non tutti, gli strumenti CLI implementano questo, ma non sono vincolati a e le informazioni fornite dipendono completamente dall'implementazione. Se invochi --helpsu una shell integrata, probabilmente otterrai "opzione non valida" e un breve messaggio di "utilizzo". Se si richiama su un autonomo che non implementa, si può anche ottenere un "opzione non valida", ma esattamente quello che succede di nuovo dipende dall'applicazione.

Se sono disponibili versioni sia integrate sia autonome di un comando e si desidera sapere quale viene utilizzato quando viene richiamato, è possibile utilizzare typeun'altra shell incorporata.

> 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

Qui possiamo vedere che sebbene esista un eseguibile autonomo echo, la echotua shell invoca un built-in.


1
Non è necessario digitare typedue volte: type -a echorestituisce tutte le chiamate a echoportata di mano (come definito da $ PATH), inclusi builtin, funzioni della shell e alias. Vedi help typeper riferimento.
Tatjana Heuser,

8

Hai già risposto alla tua domanda:

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.

L'aiuto è un comando BUILTIN (significa, bash comando interno) per ottenere informazioni su altri comandi integrati. Poiché questo script della terza parte non è un comando incorporato di bash. Se corri bash, chiama il builtin helpun uso straceotterrai:

# 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)---

Praticamente significa che queste informazioni vengono generate in fase di compilazione all'interno del binario bash.


Non sono hard coded ma vengono generati in fase di build
Ulrich Dangel l'

Grazie. (1) Cosa intendi con "chiama l'help incorporato in una sequenza di utilizzo"? (2) le informazioni di utilizzo sono hardcoded nell'eseguibile del comando integrato cd?
Tim

@UlrichDangel - Grazie per la correzione. Hard-coded si riferisce a qualcosa che è stato corretto sul codice sorgente e non all'interno del file binario durante il tempo di compilazione. Mio cattivo;) @Tim. straceè uno strumento per vedere cosa sta facendo un determinato comando durante l'esecuzione (librerie, chiamate di sistema, file aperti, ecc.). Il metodo writemostra che le informazioni della guida provengono dall'interno del file binario (bash) durante l'utilizzo del comando incorporato della guida e non dall'apertura di un file (come una pagina man).

2

Credo che l'aiuto sia parte dell'eseguibile, deve essere implementato lì. Ecco perché vedi diverse versioni di --help, a volte -h è consentita la scorciatoia, altre è "aiuto" senza prefisso ...

modificare

Ho letto male parte della tua domanda. Non ho familiarità con nessuno dei meccanismi interni del comando "help" stesso.


In che modo il comando helptrova le informazioni di un altro comando?
Tim
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.