bash自动补全脚本目录:/etc/bash_completion.d
# bash completion for menu.py
# 命令补全
_test_options()
{
local curr_arg;
curr_arg=${COMP_WORDS[COMP_CWORD]}
if echo ${COMP_WORDS[1]} | grep -Eq "menu\.py$" && [ $COMP_CWORD -eq 2 ]; then
COMPREPLY=( $(compgen -W "CheckGameStatus PushCode CodeUpdate ServiceHotCmd StopGame StartGame RestartGame" $curr_arg ) );
fi
}
complete -F _test_options -o default python python3
说明:
COMP_WORDS:命令行全部参数
COMP_CWORD:当前光标所在参数索引
${COMP_WORDS[COMP_CWORD]}:当前参数
COMPREPLY:按下tab键执行的命令
命令解释:complete -F _test_options -o default python python3
当首个参数为python或者python3时,先匹配_test_options函数,如果没有匹配,则使用默认tab匹配结果(-o default)
在_test_options函数中使用正则判断,当第一个参数为menu.py,并且光标在第二个参数时,触发特殊的命令补全,提示参数。
compgen -W {完整的字符串} {需要补全的字符串}
complete 用于绑定命令与补全函数