1、通过shell脚本完成一个linux命令,并在执行时可使用tab键补全命令和参数
2、参数分为一级参数和二级参数,并针对不同的一级参数有二级参数
通过complete,compgen,shift、eval等linux命令实现
init.d : 命令逻辑实现
test_complete : 命令主入口
.test_complete.bash : 实现命令补全
test_complete
- #!/bin/bash
- export TOP_BASE_PATH=`cd $(dirname $BASH_SOURCE); pwd`
-
- COMMAND=$1
-
- if [[ ! -f $TOP_BASE_PATH/init.d/$COMMAND ]];then
- echo "输入命令不存在"
- exit 1
- fi
-
- for cmd in `ls $TOP_BASE_PATH/init.d/`
- do
- eval "function $cmd {
- . $TOP_BASE_PATH/init.d/$cmd \$@
- }"
- done
-
- shift
- $COMMAND $@
.test_complete.bash
- _do_completions() {
- . test_complete profile
- if [ $COMP_CWORD == 1 ]; then
- COMPREPLY=($(compgen -W "$(ls ${COMMAND_PATH} --hide profile --hide option)" "${COMP_WORDS[1]}"))
- elif [ $COMP_CWORD == 2 ]; then
- COMPREPLY=($(compgen -W "$(${COMMAND_PATH}/option ${COMP_WORDS[1]})" "${COMP_WORDS[2]}"))
- fi
- }
-
- complete -F _do_completions test_complete
init.d
- # cat init.d/profile
- #!/bin/bash
-
- export TOP_BASE_PATH=`cd $(dirname $BASH_SOURCE)/../.; pwd`
- export COMMAND_PATH=$TOP_BASE_PATH/init.d
-
-
-
-
- # cat init.d/option
- #!/bin/bash
-
- option=$1
-
- if [[ $option == "change" ]];then
- echo "a b c"
- fi
-
-
- # cat init.d/start
- #!/bin/bash
- echo "=====start====="
-
-
-
- # cat init.d/stop
- #!/bin/bash
- echo "=====stop====="
-
-
-
- # cat init.d/change
- #!/bin/bash
- pararm=$1
- echo "=====$pararm====="
-
-
shell编程-使用complete实现linux命令自动补全_wwwlyj123321的博客-CSDN博客_complete命令