Article
· Jun 30, 2016 6m read

Linux bash completions for ccontrol/csession

I want to show my script which I use for a completion in bash, for ccontrol and csession. 

With this script bash helps to available (running or down) instances to ccontrol, or in csession

file
/usr/share/bash-completion/completions/ccontrol

# bash completions for InterSystems ccontrol

_instances() 
{
    if [ $# == 0 ] || [ $1 == "all" ]; then
        echo $(ccontrol qlist | cut -d'^' -f1 | tr '\n' ' ')
    else 
        echo $(ccontrol qlist | grep "\^$1\," | cut -d'^' -f1 | tr '\n' ' ')
    fi
}

_ccontrol()
{
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    commands="start stop force backup stat create update delete rename default view list qlist all qall nodename session version help"
    
    if [[ ${COMP_CWORD} == 1 ]] ; then
        COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
        return 0 
    fi

    command="${COMP_WORDS[1]}"

    parameters=""
    case "${command}" in 
        start)
            instances=$(_instances down)
            parameters="quietly nostu help"
            ;;
        stop)
            instances=$(_instances running)
            parameters="quietly nouser bypass restart nofailover help"
            ;;
        force)
            instances=$(_instances running)
            parameters="quietly help"
            ;;
        backup)
            instances=$(_instances running)
            parameters="quietly help"
            ;;
        stat)
            instances=$(_instances running)
            parameters="-h -? "
            ;;
        create)
            instances="NEWINSTANCE"
            parameters="directory= versionid="
            ;;
        update)
            instances=$(_instances all)
            parameters="versionid="
            ;;
        delete)
            instances=$(_instances all)
            parameters=""
            ;;
        rename)
            instances=$(_instances all)
            parameters="NEWNAME"
            ;;
        default)
            instances=$(_instances all)
            ;;
        view)
            instances=$(_instances all)
            ;;
        list)
            instances=$(_instances all)
            ;;
        qlist)
            instances=$(_instances all)
            ;;
        session)
            instances=$(_instances running)
            parameters="-B -U -b"
            if [[ ${COMP_CWORD} > 3 ]] && [[ ${COMP_WORDS[COMP_CWORD-1]} == "-U" ]] ; then
                namespaces="USER SAMPLES %SYS"
                    COMPREPLY=( $(compgen -W "${namespaces^^}" -- ${cur^^}) )
                return 0
            fi
            ;;
        help) 
            COMPREPLY=( $(compgen -W "${commands}" -- ${cur}))
            return 0
            ;;
        *)
            return 0
            ;;
    esac

    if [[ ${COMP_CWORD} == 2 ]] ; then
        COMPREPLY=( $(compgen -W "${instances^^}" -- ${cur^^}))
    fi

    if [[ ${parameters} == "" ]] ; then 
        return 0
    fi

    if [[ ${COMP_CWORD} > 2 ]]; then 
        COMPREPLY=( $(compgen -W "${parameters}" -- ${cur}) )
    fi

    return 0
} 
complete -F _ccontrol ccontrol

and 
/usr/share/bash-completion/completions/csession

# bash completions for InterSystems csession

_csession()
{
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    
    if [[ ${COMP_CWORD} == 1 ]] ; then
        instances=`ccontrol qlist | grep running | cut -d'^' -f1 | tr '\n' ' '`
        COMPREPLY=( $(compgen -W "${instances^^}" -- ${cur^^}) )
        return 0 
    fi

    if [[ ${COMP_CWORD} == 2 ]] ; then
    parameters="-B -U -b"
        COMPREPLY=( $(compgen -W "${parameters}" -- ${cur}) )
    return 0
    fi

    if [[ ${COMP_CWORD} == 3 ]] ; then
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    if [[ ${prev} == "-U" ]] ; then
        namespaces="USER SAMPLES %SYS"
            COMPREPLY=( $(compgen -W "${namespaces^^}" -- ${cur^^}) )
        return 0
    fi
    return 0
    fi
} 
complete -F _csession csession

you may activate bash-completions, if you still had not activated yet, with this lines in your /etc/bashrc or ~/.bashrc file

# Use bash-completion, if available
[[ $PS1 && -f /usr/share/bash-completion/bash_completion ]] && \
    . /usr/share/bash-completion/bash_completion

more information about bash completion you can find here

Discussion (2)2
Log in or sign up to continue