0
我有以下的Z shell脚本来启动一个程序的实例,如果一个程序还没有被主动运行,即使有僵尸实例,或者恢复一个停止的实例。我觉得必须有更好的方式来使用,也许使用perl。 shell脚本看起来太尴尬 - 至少应该可以从另一种语言(如perl或awk)进行文本操作。我该如何替换这个shell脚本?
launchprogram(){
if [ $# = 0 ]
then
cat <<\EOF
launchprogram requires at least one argument.
Usage: launchprogram <program> <optional arguments>
EOF
return 1
fi
mystatus=Z # assume we have a zombie process
process="$(pgrep "$1" | tr \\n ,)"
echo "$process"
process="${process%,}"
if [ "$process" != '' ]
then
process="$(ps -o 'pid s cmd' -p "$process" | sed '1 d')"
fi
oldifs="$IFS"
IFS="$(printf \nX)"
IFS="${IFS%X}"
for i in $process
do
mystatus="${process[2]}"
case $mystatus in
(T)
if ! kill -CONT "${i[1]}"
then
IFS="$oldifs"
return $?
fi
;;
(Z)
;;
(*)
IFS="$oldifs"
return $?
;;
esac
done
IFS="$oldifs"
setopt nobgnice
"[email protected]" >/dev/null 2>&1 &!
unsetopt nobgnice
}
也许这是更好地做任何文本操作可言,但使用CPAN模块'PROC :: ProcessTable'。 –
显示要替换的部分的一些示例输入和预期输出(即,用于所需的awk脚本)。 –