I've wrote a simple bash completion script to complete ssh servernames from a list, eg:
_ssh()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(cat $hosts | awk -F';' '{print $2}')
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _ssh ssh
the $opts list is sth like:
locationX-apache-01.local.lan
locationY-apache-02.local.lan
locationX-mysql-01.local.lan
locationY-mysql-02.local.lan
Bash completion is working as expected if I write eg $ ssh loca<TAB>
... But how can I get suggestions if I don't start from the beginning like this:
$ ssh apac<TAB>
to get the 2 apache servers from the list above:
locationX-apache-01.local.lan
locationY-apache-02.local.lan