完整命令有什么用?

Avi*_*Raj 21 command-line bash

complete在我的 gnome 终端上运行 命令时,它显示了一些命令。它们是什么?还有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
Run Code Online (Sandbox Code Playgroud)

列表很长,所以我发布了其中的一些。

cha*_*aos 21

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

示例:如果您键入:

user@host:~$ pidof <tab><tab>
Run Code Online (Sandbox Code Playgroud)

...出现一个列表,其中包含此命令的所有可能值。在这种情况下,它意味着所有正在运行的进程。查看complete函数的输出:

user@host:~$ complete | grep pidof
complete -F _pgrep pidof
Run Code Online (Sandbox Code Playgroud)

这意味着功能_pgrep(-F) 在使用 Tab 键命令时执行pidof。这个函数的定义在/etc/bash_completion.d/procps.

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

user@host:~$ cd /usr/<tab><tab>
bin/     games/   include/ lib/     lib32/   local/   sbin/    share/   src/
Run Code Online (Sandbox Code Playgroud)

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

自己动手:你有一个被调用的程序/脚本/bin/myprog,如果你按如下方式执行它,你想要它

user@host:~$ myprog /home/user/<tab><tab>
Run Code Online (Sandbox Code Playgroud)

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

user@host:~$ complete -F _cd myprog
Run Code Online (Sandbox Code Playgroud)

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


Syl*_*eau 10

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.
Run Code Online (Sandbox Code Playgroud)

检查/usr/share/bash-completion/bash_completion以查看 bash 附带的默认完成。

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