Question

I'm something of an amateur when it comes to Bash shell scripting and am baffled by this first line in the killproc() function in /etc/init.d/functions on a CentOS 5.10 system:

local RC killlevel= base pid pid_file= delay try

What is the meaning of that line? I believe it is the declaration of local variables perhaps, but what is the meaning of the = after a couple of the items? I have included the complete killproc() function below for reference:

# A function to stop a program.
killproc() {
    local RC killlevel= base pid pid_file= delay try

    RC=0; delay=3; try=0
    # Test syntax.
    if [ "$#" -eq 0 ]; then
        echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
        return 1
    fi
    if [ "$1" = "-p" ]; then
        pid_file=$2
        shift 2
    fi
    if [ "$1" = "-d" ]; then
        delay=$(echo $2 | awk -v RS=' ' -v IGNORECASE=1 '{if($1!~/^[0-9.]+[smhd]?$/) exit 1;d=$1~/s$|^[0-9.]*$/?1:$1~/m$/?60:$1~/h$/?60*60:$1~/d$/?24*60*60:-1;if(d==-1) exit 1;delay+=d*$1} END {printf("%d",delay+0.5)}')
        if [ "$?" -eq 1 ]; then
            echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
            return 1
        fi
        shift 2
    fi


    # check for second arg to be kill level
    [ -n "${2:-}" ] && killlevel=$2

        # Save basename.
        base=${1##*/}

        # Find pid.
    __pids_var_run "$1" "$pid_file"
    if [ -z "$pid_file" -a -z "$pid" ]; then
        pid="$(__pids_pidof "$1")"
    fi

        # Kill it.
        if [ -n "$pid" ] ; then
                [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
        if [ -z "$killlevel" ] ; then
               if checkpid $pid 2>&1; then
               # TERM first, then KILL if not dead
               kill -TERM $pid >/dev/null 2>&1
               usleep 100000
               if checkpid $pid ; then
                try=0
                while [ $try -lt $delay ] ; do
                    checkpid $pid || break
                    sleep 1
                    let try+=1
                done
                if checkpid $pid ; then
                    kill -KILL $pid >/dev/null 2>&1
                    usleep 100000
                fi
               fi
                fi
            checkpid $pid
            RC=$?
            [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
            RC=$((! $RC))
        # use specified level only
        else
                if checkpid $pid; then
                        kill $killlevel $pid >/dev/null 2>&1
                RC=$?
                [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
            elif [ -n "${LSB:-}" ]; then
                RC=7 # Program is not running
            fi
        fi
    else
        if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
            RC=7 # Program is not running
        else
            failure $"$base shutdown"
            RC=0
        fi
    fi

        # Remove pid file if any.
    if [ -z "$killlevel" ]; then
            rm -f "${pid_file:-/var/run/$base.pid}"
    fi
    return $RC
}
Was it helpful?

Solution

It's just declaring a lot of function scope variables, where killlevel and pid_file are assigned the empty string and the others are not assigned anything.

There is a slight difference. local variables are also visible in functions called from the function. So if you happen to call killproc() recursively, the uninitialized ones are remembered from the caller.

On Debian the lib/lsb/init-functions file looks like this:

killproc() {
    local pidfile sig status base name_param is_term_sig OPTIND
    pidfile=                                                   
    name_param=                                                
    is_term_sig=                                               
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top