我创建了一个bash
计算自身已启动实例的脚本。
就是这样(在这个例子中,我显示的是实例,而不是用wc -l
) :
#!/bin/bash
nb=`ps -aux | grep count_itself.sh`
echo "$nb"
sleep 20
(当然,我的脚本名为count_itself.sh
)
执行它后,我希望它返回两行,但它返回三行:
root@myserver:/# ./count_itself.sh
root 16225 0.0 0.0 12400 1176 pts/0 S+ 11:46 0:00 /bin/bash ./count_itself.sh
root 16226 0.0 0.0 12408 564 pts/0 S+ 11:46 0:00 /bin/bash ./count_itself.sh
root 16228 0.0 0.0 11740 932 pts/0 S+ 11:46 0:00 grep count_itself.sh
执行后&
标志(即在后台,并手动执行ps -aux
位,它返回两个,这就是我想要的:
root@myserver:/# ./count_itself.sh &
[1] 16233
root@myserver:/# ps -aux | grep count_itself.sh
root 16233 0.0 0.0 12408 1380 pts/0 S 11:48 0:00 /bin/bash ./count_itself.sh
root 16240 0.0 0.0 11740 944 pts/0 S+ 11:48 0:00 grep --color=auto count_itself.sh
我的问题是:为什么ps -aux
脚本内的执行返回比预期多一行?
或者,换句话说,为什么进程的id是16226
在我的第一个例子中创建?
编辑(因为大多数人似乎误解了我的问题):
我想知道为什么bash
执行返回两个实例/bin/bash ./count_itself.sh
,不是它返回的原因grep count_itself.sh
.
EDIT 2 :
当然,我正在寻找一种方法来避免这种行为并使脚本返回/bin/bash ./count_itself.sh
只有一次。