completeコマンドによる補完以下はsshコマンドを入力する際に.ssh/known_hostsからホスト名を抜き出して補完する。.bashrc等に設定する。 function ssh_complete() {
local curw
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
ORIG=$IFS
IFS="
"
COMPREPLY=($(compgen -W '$(cut -f 1 -d " " $HOME/.ssh/known_hosts)' -- $curw))
IFS=$ORIG
return 0
}
complete -F ssh_complete ssh
上の修正版 function ssh_complete() {
local curw
local hosts=()
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
ORIG=$IFS
IFS="
"
if [ -f "$HOME/.ssh/known_hosts" ]; then
hosts=(${hosts[@]} $(cut -f 1 -d " " $HOME/.ssh/known_hosts))
fi
if [ -f "$HOME/.ssh/config" ]; then
hosts=(${hosts[@]} $(grep '^Host ' "$HOME/.ssh/config" | cut -f 2 -d ' '))
fi
COMPREPLY=($(compgen -W '${hosts[@]}' -- $curw))
IFS=$ORIG
return 0
}
|
|