完整命令有什么用?

完整命令有什么用?

当我在 gnome-terminal 上运行 complete命令时,它会显示一些命令。它们是什么?这些命令有什么用complete

$ complete
complete -F _minimal 
complete -F _filedir_xspec oodraw
complete -F _filedir_xspec elinks
complete -F _filedir_xspec freeamp
complete -F _longopt split
complete -F _longopt sed
complete -F _longopt ld
complete -F _longopt grep
complete -F _service /etc/init.d/vboxweb-service
complete -F _service /etc/init.d/vboxballoonctrl-service
complete -F _service /etc/init.d/rc
complete -F _service /etc/init.d/nmbd
complete -F _service /etc/init.d/halt
complete -j -P '"%' -S '"' jobs
complete -d pushd

列表很长,因此我发布了其中一些。

答案1

complete是 bash 内置函数。因此系统上没有二进制文件。它处理按下 时命令的完成方式tab

例子:如果你输入:

user@host:~$ pidof <tab><tab>

...出现一个列表,其中包含此命令的所有可能值。在本例中,它表示所有正在运行的进程。查看函数的输出complete

user@host:~$ complete | grep pidof
complete -F _pgrep pidof

_pgrep这意味着在按 Tab 键输入命令时执行函数(-F) pidof。此函数的定义位于 中/etc/bash_completion.d/procps

另一个例子:如果你输入:

user@host:~$ cd /usr/<tab><tab>
bin/     games/   include/ lib/     lib32/   local/   sbin/    share/   src/

cd...您会看到下可以访问的文件夹列表/usr/。执行了哪个函数?greping 该函数(如上所示)告诉我们它是中的complete函数。_cd/etc/bash_completion

自己做:您有一个名为的程序/脚本/bin/myprog,并且您希望按如下方式执行它

user@host:~$ myprog /home/user/<tab><tab>

...它应该只列出文件夹,而不是文件。因此,使用以下命令扩展您的 bash 补全:

user@host:~$ complete -F _cd myprog

就是这样。您还可以编写自己的函数来完成自定义的事情,例如仅完成特定的文件或数字或静态值列表...

答案2

complete是一个 bash 命令,用于在用户TAB在终端中按下键时执行自动完成操作。

调用 justcomplete将列出所有注册的用于自动完成命令或服务选项的函数。

来自 bash 手册页:

complete: complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat]
          [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix]
          [-S suffix] [name ...]
    Specify how arguments are to be completed by Readline.

    For each NAME, specify how arguments are to be completed.  If no options
    are supplied, existing completion specifications are printed in a way that
    allows them to be reused as input.

    Options:
      -p    print existing completion specifications in a reusable format
      -r    remove a completion specification for each NAME, or, if no
        NAMEs are supplied, all completion specifications
      -D    apply the completions and actions as the default for commands
        without any specific completion defined
      -E    apply the completions and actions to "empty" commands --
        completion attempted on a blank line

    When completion is attempted, the actions are applied in the order the
    uppercase-letter options are listed above.  The -D option takes
    precedence over -E.

    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

检查/usr/share/bash-completion/bash_completionbash 自带的默认补全。

访问http://www.linuxjournal.com/content/more-using-bash-complete-command有关此命令的完整教程。

答案3

(另请参阅:https://www.reddit.com/r/bash/comments/qyr8sj/understanding_c_command_for_completions/ https://echorand.me/posts/linux_shell_autocompletion/

来自man bash搜索‘完成’:

Completing
       complete (TAB)
              Attempt to perform completion on the text before point.  Bash attempts comple-
              tion treating the text as a variable (if the text begins with $), username (if
              the text begins with ~), hostname (if the text begins with @), or command (in-
              cluding aliases and functions) in turn.  If none of these  produces  a  match,
              filename completion is attempted.
       possible-completions (M-?)
              List the possible completions of the text before point.
       insert-completions (M-*)
              Insert all completions of the text before point that would have been generated
              by possible-completions.
       menu-complete
              Similar to complete, but replaces the word to be completed with a single match
              from  the  list  of possible completions.  Repeated execution of menu-complete
              steps through the list of possible completions, inserting each match in  turn.
              At  the  end of the list of completions, the bell is rung (subject to the set-
              ting of bell-style) and the original text is restored.  An argument of n moves
              n positions forward in the list of matches; a negative argument may be used to
              move backward through the list.  This command is intended to be bound to  TAB,
              but is unbound by default.
       menu-complete-backward
              Identical  to  menu-complete,  but moves backward through the list of possible
              completions, as if menu-complete had been given  a  negative  argument.   This
              command is unbound by default.
       delete-char-or-list
              Deletes  the  character under the cursor if not at the beginning or end of the
              line (like delete-char).  If at the end of the line,  behaves  identically  to
              possible-completions.  This command is unbound by default.
       complete-filename (M-/)
              Attempt filename completion on the text before point.
       possible-filename-completions (C-x /)
              List the possible completions of the text before point, treating it as a file-
              name.
       complete-username (M-~)
              Attempt completion on the text before point, treating it as a username.
       possible-username-completions (C-x ~)
              List the possible completions of the text before point, treating it as a user-
              name.
       complete-variable (M-$)
              Attempt completion on the text before point, treating it as a shell variable.
       possible-variable-completions (C-x $)
              List the possible completions of the text before point, treating it as a shell
              variable.
       complete-hostname (M-@)
              Attempt completion on the text before point, treating it as a hostname.
       possible-hostname-completions (C-x @)
              List the possible completions of the text before point, treating it as a host-
              name.
       complete-command (M-!)
          Attempt  completion  on  the text before point, treating it as a command name.
          Command completion attempts to match the text against aliases, reserved words,
          shell functions, shell builtins, and finally executable filenames, in that or-
          der.
       possible-command-completions (C-x !)
          List the possible completions of the text before point, treating it as a  com-
          mand name.
       dynamic-complete-history (M-TAB)
          Attempt  completion on the text before point, comparing the text against lines
          from the history list for possible completion matches.
       dabbrev-expand
          Attempt menu completion on the text before point, comparing the  text  against
          lines from the history list for possible completion matches.
       complete-into-braces (M-{)
          Perform  filename  completion  and insert the list of possible completions en-
          closed within braces so the list is available to the shell (see  Brace  Expan-
          sion above).

搜索“可编程完成”:

Programmable Completion
       When word completion is attempted for an argument to a command for which a completion
       specification  (a  compspec)  has  been defined using the complete builtin (see SHELL
       BUILTIN COMMANDS below), the programmable completion facilities are invoked.

       First, the command name is identified.  If the command word is the empty string (com-
       pletion  attempted  at the beginning of an empty line), any compspec defined with the
       -E option to complete is used.  If a compspec has been defined for that command,  the
       compspec  is  used to generate the list of possible completions for the word.  If the
       command word is a full pathname, a compspec for the full  pathname  is  searched  for
       first.   If  no compspec is found for the full pathname, an attempt is made to find a
       compspec for the portion following the final slash.  If those searches do not  result
       in a compspec, any compspec defined with the -D option to complete is used as the de-
       fault.  If there is no default compspec, bash attempts alias expansion on the command
       word as a final resort, and attempts to find a compspec for the command word from any
       successful expansion.

       Once a compspec has been found, it is used to generate the list  of  matching  words.
       If a compspec is not found, the default bash completion as described above under Com-
       pleting is performed.

       First, the actions specified by the compspec are used.  Only matches which  are  pre-
       fixed by the word being completed are returned.  When the -f or -d option is used for
       filename or directory name completion, the shell variable FIGNORE is used  to  filter
       the matches.

       Any completions specified by a pathname expansion pattern to the -G option are gener-
       ated next.  The words generated by the pattern need not match  the  word  being  com-
       pleted.   The  GLOBIGNORE  shell  variable is not used to filter the matches, but the
       FIGNORE variable is used.

       Next, the string specified as the argument to  the  -W  option  is  considered.   The
       string is first split using the characters in the IFS special variable as delimiters.
       Shell quoting is honored.  Each word is then expanded using  brace  expansion,  tilde
       expansion, parameter and variable expansion, command substitution, and arithmetic ex-
       pansion, as described above under EXPANSION.  The results are split using  the  rules
       described  above  under  Word  Splitting.   The  results of the expansion are prefix-
       matched against the word being completed, and the matching words become the  possible
       completions.

       After these matches have been generated, any shell function or command specified with
       the -F and -C options is invoked.  When the  command  or  function  is  invoked,  the
       COMP_LINE,  COMP_POINT,  COMP_KEY, and COMP_TYPE variables are assigned values as de-
       scribed above under Shell Variables.  If a  shell  function  is  being  invoked,  the
       COMP_WORDS  and  COMP_CWORD  variables are also set.  When the function or command is
       invoked, the first argument ($1) is the name of the command whose arguments are being
       completed,  the second argument ($2) is the word being completed, and the third argu-
       ment ($3) is the word preceding the word being completed on the current command line.
       No  filtering  of  the generated completions against the word being completed is per-
       formed; the function or command has complete freedom in generating the matches.

       Any function specified with -F is invoked first.  The function may  use  any  of  the
       shell  facilities,  including  the  compgen  builtin described below, to generate the
       matches.  It must put the possible completions in the COMPREPLY array  variable,  one
       per array element.

       Next,  any  command specified with the -C option is invoked in an environment equiva-
       lent to command substitution.  It should print a list of completions, one  per  line,
       to the standard output.  Backslash may be used to escape a newline, if necessary.

       After all of the possible completions are generated, any filter specified with the -X
       option is applied to the list.  The filter is a pattern as used for  pathname  expan-
       sion;  a  &  in the pattern is replaced with the text of the word being completed.  A
       literal & may be escaped with a backslash; the backslash is removed before attempting
       a  match.   Any completion that matches the pattern will be removed from the list.  A
       leading ! negates the pattern; in this case any completion not matching  the  pattern
       will  be removed.  If the nocasematch shell option is enabled, the match is performed
       without regard to the case of alphabetic characters.

       Finally, any prefix and suffix specified with the -P and -S options are added to each
       member  of the completion list, and the result is returned to the readline completion
       code as the list of possible completions.

       If the previously-applied actions do not generate any matches, and  the  -o  dirnames
       option was supplied to complete when the compspec was defined, directory name comple-
       tion is attempted.

       If the -o plusdirs option was supplied to complete when the compspec was defined, di-
       rectory  name completion is attempted and any matches are added to the results of the
       other actions.

       By default, if a compspec is found, whatever it generates is returned to the  comple-
       tion  code as the full set of possible completions.  The default bash completions are
       not attempted, and the readline default of filename completion is disabled.   If  the
       -o  bashdefault  option  was  supplied to complete when the compspec was defined, the
       bash default completions are attempted if the compspec generates no matches.  If  the
       -o  default option was supplied to complete when the compspec was defined, readline's
       default completion will be performed if the compspec (and, if attempted, the  default
       bash completions) generate no matches.

       When a compspec indicates that directory name completion is desired, the programmable
       completion functions force readline to append a slash to completed  names  which  are
       symbolic  links to directories, subject to the value of the mark-directories readline
       variable, regardless of the setting of the mark-symlinked-directories readline  vari-
       able.

       There  is  some  support  for dynamically modifying completions.  This is most useful
       when used in combination with a default completion specified with complete -D.   It's
       possible for shell functions executed as completion handlers to indicate that comple-
       tion should be retried by returning an exit status of 124.  If a shell  function  re-
       turns  124,  and changes the compspec associated with the command on which completion
       is being attempted (supplied as the first argument when the  function  is  executed),
       programmable  completion  restarts  from the beginning, with an attempt to find a new
       compspec for that command.  This allows a set of completions to be built  dynamically
       as completion is attempted, rather than being loaded all at once.

       For instance, assuming that there is a library of compspecs, each kept in a file cor-
       responding to the name of the command,  the  following  default  completion  function
       would load completions dynamically:

       _completion_loader()
       {
            . "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return 124
       }
       complete -D -F _completion_loader -o bashdefault -o default

答案4

所以。这里的所有答案都很好,但也有些不完整。(双关语)
我觉得有必要进行回顾,现在就回顾一下。

1. 在哪里可以找到 bash 文档

首先我们来看一下 bash 文档。总的来说。

您可以从以下位置获取有关 bash 的文档:

  • bash 手册页,
  • 内联帮助,
  • 信息手册。

bash 手册页糟透了总是这样。这是故意的。非常肤浅。是设计使然。

help <command>通过在命令行中输入内容即可获得内联帮助。如果你不记得语法,内联帮助很有用。尽管如此,它还是很不完整。相比之下,在 Windows 上,你几乎可以cmd.exe从内联帮助中了解所有内容。但 Bash 就没那么好了。

信息手册才是真正的重点。你可以info bash从命令行调用它。你可以输入搜索它,/<query>它有章节和导航,但 bash 手册是不是整齐地划分了每个命令的章节,而你只能依靠/<name-of-the-command>反复点击,直到找到正确的点。乏味

,你可能会不是信息阅读器安装后,信息文档一般而言,或者bash 信息文档特别是。为什么?包管理器的行为很神秘,我们普通用户没有资格质疑他们的逻辑(除非我们想最终制作另一个发行版正在进行中)。

如果你没有安装信息文档,调用info bash将会显示其不太理想手册页。唉。但是Debian/Ubuntu/kali您只需要apt install bash-doc安装它,就这样。

好消息是 bash 手册在 gnu 网站上也始终可用:
https://www.gnu.org/software/bash/manual/

您可以在那里浏览它。
有关完全的康普根, 和康普特在这儿:
https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html

2. 如何深入了解completeUbuntu 上的使用方式

正如你所注意到的,通过执行complete -pubuntu 预定义很多完成。这是不是每个发行版都很典型。许多发行版不需要安装,其余的发行版需要安装软件包才能运行。

您可能还注意到,大多数补全都是-F补全,也就是说:它们将补全的处理推迟到 bash 函数。您如何获取该函数的代码?通过另一个名为 的内置函数declare

declare    # shows all declare variables, functions, everything.
declare -f # shows all declared functions and their code.
declare -F # shows ONLY declared function NAMES

补全存储和加载到哪里?大多数补全都来自包/usr/share/bash-completion/completions并由包安装bash-completion

还有一个/etc/bash_completion文件,它只是提供文件/usr/share/bash-completion/bash_completion和目录/etc/bash_completion.d/,包可以在其中为自己的自定义命令添加自己的自定义补全(例如 git 就是这样做的)。

现在你应该有足够的数据来自己去探索完成情况,而不会感到沮丧。

相关内容