Reputation: 2003
I've created the following bash completion function:
_scpinst ()
{
local cur prev opts;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
prev="${COMP_WORDS[COMP_CWORD-1]}";
opts="-l";
if [[ ${prev} == '-l' ]]; then
files=$(ls /upgrade/*.tgz.gpg 2>/dev/null);
COMPREPLY=($(compgen -W "${files}" -- ${cur}));
compopt -o nospace
return 0;
fi;
COMPREPLY=($(compgen -W "${opts}" -- ${cur}));
return 0
}
complete -F _scpinst scpinst
It automatically completes *.tgz.gpg files in /upgrade directory after a -l flag. Is there a better way to accomplish it without using an 'ls' command. E.g. using -o filenames or -f ?
Upvotes: 3
Views: 1006
Reputation: 1773
Just use -G globpat
compgen’s option:
COMPREPLY=($(compgen -G "/upgrade/*.tgz.gpg" -- ${cur}));
If by some reason you want to use exactly -W wordlist
(e. g. to mix filenames with something else), this is also possible:
files=(/upgrade/*.tgz.gpg)
COMPREPLY=($(compgen -W '"${files[@]}"' -- ${cur}));
Both versions should properly handle filenames containing spaces (while your variant would not).
Upvotes: 4