Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to setup autocompletion for a command, which takes only one parameter, a folder name,

In /secure/kernel_builds/, I have the three following subfolder:

3.5.6/ 3.6.2/ source/

Now I want auto completion, that lists all sub folder name possible, but not the one named source, right now I'm using a pretty stupid way to do it, by checking if the completion word is started with a number,

_avail_kernel_builds()
{
    case $COMP_CWORD in
        1)
            if [[ "${COMP_WORDS[COMP_CWORD]}" =~ ^[0-9] ]];then
                COMPREPLY=( /secure/kernel_builds/"${COMP_WORDS[COMP_CWORD]}"* )
            else
                COMPREPLY=( /secure/kernel_builds/"${COMP_WORDS[COMP_CWORD]}"[0-9]*/ )
            fi
            COMPREPLY=( "${COMPREPLY[@]#/secure/kernel_builds/}" )
            COMPREPLY=( "${COMPREPLY[@]%/}" )
            ;;
    esac    
}

Is there a better way of doing so?

share|improve this question
add comment

1 Answer

You can check in a loop for the strings you (don't) want:

declare -a accept
declare -a reject
for s in "${COMPREPLY[@]}"; do
    if [[ "$s" =~ want ]]; then
        accept=( "${accept[@]}" "$s" )
    else
        reject=( "${reject[@]}" "$s" )
    fi
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.