【发布时间】:2012-04-19 14:11:55
【问题描述】:
我得到一段 PID 文件控制的代码。
程序员的作风,我不懂..
我不知道 -->
&& 的使用
[[ $mypid -ne $procpid ]] **&&**
然后正确地重新启动自己(不适用于 MacosX)
$0 $@ &
代码完成...
function createpidfile() {
mypid=$1
pidfile=$2
#Close stderr, don't overwrite existing file, shove my pid in the lock file.
$(exec 2>&-; set -o noclobber; echo "$mypid" > "$pidfile")
[[ ! -f "$pidfile" ]] && exit #Lock file creation failed
procpid=$(<"$pidfile")
[[ $mypid -ne $procpid ]] && {
#I'm not the pid in the lock file
# Is the process pid in the lockfile still running?
isrunning "$pidfile" || {
# No. Kill the pidfile and relaunch ourselves properly.
rm "$pidfile"
$0 $@ &
}
exit
}
}
我迷路了
【问题讨论】:
-
该代码看起来不错。我不明白他们为什么像他们那样做了函数的第 4 行。这应该是一个错误,除非其中一个命令产生一个有效的命令名称,它们不应该,并且应该引用它而不是依赖于分词。他们可能意味着它是一个子shell,前面没有
$符号来隔离noclobber。另外,不要使用function name() {语法。如果是 Bash,请使用name()。如果您正在做一些不寻常的 ksh/bash 多语言库,请使用function name {。 -
要正确保护带有空格的参数,请将
$0 $@ &更改为"$0" "$@" &-- gnu.org/software/bash/manual/bashref.html#Special-Parameters