运行 一个 shell 脚本来获取正确的进程数

Running a shell script to get correct process count

我正在尝试 运行 下面的 shell 脚本 test.sh

$service=
$count=`ps -ef |grep -i "$service" |grep -v grep | wc -l`
echo "$count"

命令:sh test.sh abcde

我希望脚本输出 0 但它给了我 1。

PS:我将使用 php 文件中的 shell_exec 运行 宁此脚本,脚本的输入将是来自 php 的数组元素文件

你得到 1 因为 ps -ef 的输出包含命令

sh test.sh abcde

当您执行 grep -i "abcde" 时匹配。出于与过滤掉 grep 相同的原因,您需要过滤掉它,因此更改

grep -v grep

grep -E -v 'grep|test\.sh'