我正在尝试为bash完成构建一个生成器,但是我需要能够调用“嵌套”命令。这是我到目前为止所拥有的。
#!bash
_test()
{
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "plain" -- ${cur}))
;;
2)
case ${prev} in
plain)
_plain
;;
show)
COMPREPLY=($(compgen -W "some other args" -- ${cur}))
;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
_plain() {
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
echo ${COMP_CWORD}
echo $cur
echo $prev
case ${COMP_CWORD} in
2)
COMPREPLY=($(compgen -W "one two three" -- ${cur}))
;;
3)
case ${prev} in
one)
__one
;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
__one() {
echo "another"
}
complete -F _test test
包括调试输出。由于某些原因,调用COMP_CWORD
函数时cur
,prev
和__one
无法得到更新。我可以通过哪种方式完成这样的嵌套bash完成脚本?