M BUZZ CRAZE NEWS
// news

bash completion using ls

By David Jones

This works from shell:

 export files=`ls /home/tests/` complete -o filenames -W "${files}" xtest

This script doesn't :

 _xtest () { . local cur local files=`ls /home/tests` COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} #case "$cur" in COMPREPLY=( $(compgen -W "${files}" -- ${cur}) ) # esac return 0 } complete -F _xtest -o filenames xtest

What is the way using a script for autocomplete from file list of a directory ? I'm using a script because only for 1 script option (-test) is this customized autocomplete required, for other script options (-speed, -define) default autocomplete is O.K.

1 Answer

Declaring a variable and assigning it on one line can mask the return values. The use of $(..) instead of legacy backticks is prefered. Given the lack of context of your code snippit the folowing code is the best I can do with out being able to test it.

 _xtest () { local cur local files files=$(ls /home/tests) COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} case "$cur" in -*) COMPREPLY=( $( compgen -W $files --$cur ) );; esac return 0 } complete -F _xtest -o filenames xtest

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy