如果我对我想查找并运行脚本的过程进行硬编码
脚本A
#!/bin/bash
PROCESS=/System/Library/Frameworks/QuickLook.framework/Resources/quicklookd.app/Co
number=$(ps aux | grep $PROCESS | wc -l)
if [ $number -le 1 ]
then
echo "Nope - This seems NOT to be Running"
exit 2
else
if [ $number -ge 2 ]
then
echo "This seems to be Running"
exit 0
fi
fi
剧本给了我所期望的答案。
如果我像这样运行脚本 B ./scriptb.sh/System/Library/Frameworks/QuickLook.framework/Resources/quicklookd.app/Co
即使我将 sakdjnsakjsakfndsdkjnf 作为参数,它总是返回“这似乎正在运行”?
#!/bin/bash
number=$(ps aux | grep $1 | wc -l)
if [ $number -le 1 ]
then
echo "Nope - This seems NOT to be Running"
exit 2
else
if [ $number -ge 2 ]
then
echo "This seems to be Running"
exit 0
fi
fi
怎么了?
答案1
grep 将自身定位在进程列表中。
使用 pgrep 搜索进程。例如:
T=$(pgrep -f "$PROCESS")
if test -z "$T" ; then
...
else
...
fi
应该可以解决问题。