- .
, , .
, :
_cd(){
local shoptbakup="`shopt -p nullglob`"
shopt -s nullglob
local cur opts i opt
local IFS=$'\n'
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ "${cur:0:1}" == '~' ]]; then
cur="$HOME/${cur:1}"
fi
if [[ $cur == */* ]]; then
opts=(${cur}*/)
else
opts=(*${cur}*/)
fi
i=0
for opt in "${opts[@]}"; do
opts[$i]=${opt%/}
i=$((i+1))
done
eval "$shoptbakup" 2>/dev/null
COMPREPLY=("${opts[@]}")
}
complete -o filenames -o bashdefault -o nospace -F _cd cd
, :
$ ls
test 6/ test 7/ test 8/ test1/ test2/ test3/ test4/ test5/
$ cd 7[TAB]
:
$ cd test\ 7/
, , , , , script , . :
http://fahdshariff.blogspot.com/2011/04/writing-your-own-bash-completion.html
:
, script, listanimals. ,
_listanimals()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
if [[ $prev == "--sort-direction" ]]; then
COMPREPLY=( $( compgen -W "horizontal vertical" -- $cur ) )
return 0
elif [[ $prev == "--type" ]];then
COMPREPLY=( $( compgen -W "mammals reptiles birds fish marsupials insects misc" -- $cur ) )
return 0
elif [[ $prev == "--size" ]]; then
COMPREPLY=( $( compgen -W "tiny small medium large huge" -- $cur ) )
return 0
fi
if [[ "$cur" == --* ]] || [[ $cur == "" ]]; then
COMPREPLY=( $( compgen -W "--sort-direction --type --size" -- $cur ) )
fi
}
complete -F _listanimals listanimals
:
$ listanimals --[TAB][TAB]
--size --sort-direction --type
$ listanimals --t[TAB]
:
$ listanimals --type
:
$ listanimals --type [TAB][TAB]
birds fish insects mammals marsupials misc reptiles
And I'm sure you can figure out the rest. This is a working example, so if you want to try just copy / paste it into a file (for example, sample.sh) and specify it in bash source sample.sh. You really don't need to have script names listanimals.
Hope someone finds this helpful because I found the pain to understand.
source
share