Скажем, у меня есть простой сценарий в /usr/local/bin/myscript
как
#!/bin/bash
case "$1" in
start)
start something
;;
stop)
stop something
;;
status)
status of something
;;
*)
echo "unknown option $1"
esac
Как и где я мог предоставить те три возможности, start/stop/status, таким образом, пользователь может отобразить/автоматически заполнить их нажатие TAB
?
Как, например. apt-get
+ 2x Tab
дает мне
autoclean check dselect-upgrade source
autoremove clean install update
build-dep dist-upgrade purge upgrade
changelog download remove
Как предложено @Ravexina я включил файл /etc/bash_completion.d/myscript_comp
как
_my_script_comp ()
{
local cur # A pointer named "cur" to current completion word.
COMPREPLY=() # Array variable storing the possible completions.
cur=${COMP_WORDS[COMP_CWORD]}
# Show it for every possible combination
# we could do "s*" to only complete words starting with "s"
case "$cur" in
# Generate the completion matches and load them into $COMPREPLY array.
*) COMPREPLY=( $( compgen -W 'start status stop' -- $cur ) );;
esac
return 0
}
complete -F _my_script_comp myscript
Но whn я ввожу myscript
и нажмите 2x TAB
Я теперь получаю все файлы, перечисленные в текущем каталоге вместо start stop status
...
Создайте файл здесь:
/etc/bash_completion.d/
имя это вообще Вы хотите, например: myscript
.
Добавляют эти строки в него и сохраняют его:
_my_script_comp ()
{
local cur # A pointer named "cur" to current completion word.
COMPREPLY=() # Array variable storing the possible completions.
cur=${COMP_WORDS[COMP_CWORD]}
# Show it for every possible combination
# we could do "s*" to only complete words starting with "s"
case "$cur" in
# Generate the completion matches and load them into $COMPREPLY array.
*) COMPREPLY=( $( compgen -W 'start status stop' -- $cur ) );;
esac
return 0
}
complete -F _my_script_comp script.sh
Теперь у Вас есть свое завершение удара для /usr/local/bin/script.sh
.
Мой источник .