/lib/lsb/init-functions 有什么作用?

/lib/lsb/init-functions 有什么作用?

我需要理解以下代码的含义/lib/lsb/init-functions

 base=${1##*/}

此外,如果能够解释pidofproc函数如何将值返回到,也会很有帮助status_of_proc

编辑:

pidofproc

pidofproc () {
    local pidfile base status specified pid OPTIND
pidfile=
specified=

OPTIND=1
while getopts p: opt ; do
    case "$opt" in
        p)  pidfile="$OPTARG"
            specified="specified"
    ;;
    esac
done
shift $(($OPTIND - 1))
if [ $# -ne 1 ]; then
    echo "$0: invalid arguments" >&2
    return 4
fi

base=${1##*/}
if [ ! "$specified" ]; then
    pidfile="/var/run/$base.pid"
fi

if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then
    read pid < "$pidfile"
    if [ -n "${pid:-}" ]; then
        if $(kill -0 "${pid:-}" 2> /dev/null); then
            echo "$pid" || true
            return 0
        elif ps "${pid:-}" >/dev/null 2>&1; then
            echo "$pid" || true
            return 0 # program is running, but not owned by this user
        else
            return 1 # program is dead and /var/run pid file exists
        fi
    fi
fi
if [ -n "$specified" ]; then
    if [ -e "$pidfile" -a ! -r "$pidfile" ]; then
        return 4 # pidfile exists, but unreadable, return unknown
    else
        return 3 # pidfile specified, but contains no PID to test
    fi
fi
if [ -x /bin/pidof ]; then
    status="0"
    /bin/pidof -o %PPID -x $1 || status="$?"
    if [ "$status" = 1 ]; then
        return 3 # program is not running
    fi
    return 0
fi
return 4 # Unable to determine status
}

status_of_proc

status_of_proc () {
    local pidfile daemon name status OPTIND

pidfile=
OPTIND=1
while getopts p: opt ; do
    case "$opt" in
        p)  pidfile="$OPTARG";;
    esac
done
shift $(($OPTIND - 1))

if [ -n "$pidfile" ]; then
    pidfile="-p $pidfile"
fi
daemon="$1"
name="$2"

status="0"
pidofproc $pidfile $daemon >/dev/null || status="$?"
if [ "$status" = 0 ]; then
    log_success_msg "$name is running"
    return 0
elif [ "$status" = 4 ]; then
    log_failure_msg "could not access PID file for $name"
    return $status
else
    log_failure_msg "$name is not running"
    return $status
fi
}

答案1

答案在这里:

status="0"
pidofproc $pidfile $daemon >/dev/null || status="$?"

所以status_of_proc调用pidofprocwhich set $base。该变量值是在当前 shell 中设置的,因此当pidofproc返回到 时它的值仍然存在status_of_proc

例如:

fn1() { unset var; fn2; echo "$var"; }
fn2() { var=set; }
fn1

输出

set

在以下[测试]命令中,根据其结果pidofproc进行评估并返回:$pidfile

[ -e "$pidfile" -a ! -r "$pidfile" ]

所以这可以翻译为:

if $pidfile exists and it is not readable

全文在这里:

if [ -e "$pidfile" -a ! -r "$pidfile" ]; then
        return 4 # pidfile exists, but unreadable, return unknown
    else
        return 3 # pidfile specified, but contains no PID to test

相关内容