我正在尝试编写一个脚本来计算与patern匹配的进程数。如果它超过硬编码值,那么做一些事情......否则做其他事情。
我正在使用以下方法查找进程数:
ps ax | grep process_name | wc -l | sed -e "s: ::g"
如果上述命令的输出大于15,则应该回显“完成”。否则,回显“未完成”。
到目前为止,我有这个,但它不起作用:
numprocesses=ps ax | grep sms_queue | wc -l | sed -e "s: ::g"
if [ $numprocesses -le "15" ] ; then
echo "Done."
else
echo "Not Complete."
fi
答案 0 :(得分:9)
numprocesses=$(ps ax | grep '[s]ms_queue' | wc -l)
if [[ $numprocesses -gt 15 ]] ; then
echo "Done."
else
echo "Not Complete."
fi
你遇到了一些问题。
xyz
命令的输出,您应该使用$(xyz)
。grep
进程(因为它也有它正在寻找的模式),您应该使用[firstchar]rest
grep模式(或者您可以使用{ {1}}也可以从计数中删除| grep sms_queue | grep -v grep
进程。答案 1 :(得分:1)
如果要将命令的输出复制到变量中,请使用以下语法:
variable=$(my command)
答案 2 :(得分:0)
怎么样
pgrep -c sms_queue
整个(便携式)脚本版本如下所示:
if [ "$(pgrep -c sms_queue)" -le 15 ]; then
echo "Done."
else
echo "Not Complete."
fi