/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呼び出してください。変数の値は現在のシェルに設定されているため、を返すときにその値は存在し続けます。pidofproc$basepidofprocstatus_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

おすすめ記事