Предположим, у меня есть простой скрипт в /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
Но когда я набираю 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
Теперь у вас есть завершение bash для /usr/local/bin/script.sh.
Мой источник.
Создайте файл здесь:
/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
Теперь у вас есть завершение bash для /usr/local/bin/script.sh.
Мой источник.
Создайте файл здесь:
/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
Теперь у вас есть завершение bash для /usr/local/bin/script.sh.
Мой источник.