다음 코드의 의미를 이해해야 합니다 /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
$base
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