Type less, smile more

How many times a week do you type /etc/init.d/blah start and then you realize you’re on HP-UX and it’s /sbin/init.d/blah Forget it.

# Turn on extended globbing and programmable completion
shopt -s extglob progcomp

export MYOS=$(uname -s)

if [[ "${MYOS}" = "Linux" || "${MYOS}" = "SunOS" ]] ; then
    INITDIR='/etc/init.d';
else
    INITDIR='/sbin/init.d';
fi
export INITDIR;

function RCCT () {.
    if [[ -z "${2}" ]] ; then.
        ls $INITDIR; 
    elif [[ "${1}" = "Start" ]] ; then
        cd / && $INITDIR/${2} start ; cd -;
    elif [[ "${1}" = "Restart" ]] ; then
        cd / && $INITDIR/${2} restart ; cd -;
    elif [[ "${1}" = "Stop" ]] ; then
        cd / && $INITDIR/${2} stop ; cd -;
    elif [[ "${1}" = "Reload" ]] ; then
        cd / && $INITDIR/${2} reload ; cd -;
    else
        echo "Something bad happened.";
    fi
}   

function _myservices() {
    local cur
    cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $( builtin echo $INITDIR/!(*.rpmsave|*.rpmorig|*.dpkg-old|*~|functions)) )  
    COMPREPLY=( $( compgen -W '${COMPREPLY[@]#@($INITDIR)/}' -- $cur) )
}

function Start () { RCCT ${FUNCNAME} ${*} ; };
function Restart () { RCCT ${FUNCNAME} ${*} ; };
function Reload () { RCCT ${FUNCNAME} ${*} ; };
function Stop () { RCCT ${FUNCNAME} ${*} ; };
complete -F _myservices Start Restart Reload Stop

You might be thinking that RCCT is pretty ugly and it is, but it’s complete-able, so it all shakes out. It also starts and stops services with / as the working directory which is a good idea if you’re dealing with Solaris or HP-UX.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.