如何在 SLES 12 中自动完成命令的选项?

How to autocomplete the options for a command in SLES 12?

我有一项服务,比如 xyz。截至目前,我可以分别使用命令 rcxyz start、rcxyz stop 和 rcxyz restart 来启动、停止或重新启动该服务。它工作正常。

我想做的就是,如果我键入 rcxyz 并按 <TAB>,我希望自动完成三个选项(开始、停止和重新启动)。

我参考了 Whosebug 中的其他 bash_completion 相关问题并尝试编写一个虚拟脚本 /etc/bash_completion.d/rcxyz.sh 我在其中编写了

complete -c rcxyz

我原以为当我键入 rcxyz 并按 <TAB> 时,我将获得可用命令列表作为参数。但是没用。

我是不是漏掉了什么?

你应该写:

complete -W 'start stop restart' rcxyz

参见bash的手册:

-W wordlist

The wordlist is split using the characters in the IFS special variable as delimiters, and each resultant word is expanded. The possible completions are the members of the resultant list which match the word being completed.