Bash脚本错误地计数了自己的实例 [英] Bash script counting instances of itself wrongly

查看:74
本文介绍了Bash脚本错误地计数了自己的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个bash脚本,该脚本可以计算其自身启动的实例.

I've created a bash script which counts launched instances of itself.

在这里(在此示例中,我显示的是实例,而不是用wc -l进行计数):

Here it is (in this example, I'm showing the instances rather than counting them with wc -l) :

#!/bin/bash
nb=`ps -aux | grep count_itself.sh`
echo "$nb"
sleep 20

(当然,我的脚本名为count_itself.sh)

(Of course, my script is named count_itself.sh)

执行它后,我希望它返回两行,但返回三行:

Upon executing it, I expect it to return two lines, but it returns three :

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位,它返回两个,这就是我想要的:

Upon executing it with the & flag (i.e. in background, and manually executing the ps -aux bit, it returns two, which is what I want :

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执行返回的行比预期的多一行?

My question is : Why does the ps -aux execution inside the script return one line more than expected ?

或者换句话说,为什么在我的第一个示例中创建了ID为16226的进程?

Or, in another words, why is the process with the id 16226 created in my first example ?

编辑(大多数人似乎误会了我的问题):

我想知道为什么bash执行返回两个/bin/bash ./count_itself.sh实例,而不是为什么它返回grep count_itself.sh.

I'm wondering why the bash execution returns two instances of /bin/bash ./count_itself.sh, not why it returns grep count_itself.sh.

当然,我正在寻找一种方法来避免这种行为,并使脚本仅返回一次/bin/bash ./count_itself.sh.

And of course, I'm looking for a way to avoid this behaviour and have the script return /bin/bash ./count_itself.sh only once.

推荐答案

终于找到了一种方法,尽管它很丑陋,但其部分灵感来自他的

Finally found a way, albeit an ugly one, partially inspired from the question @TomFenech linked in his answer:

#!/bin/bash
nb=$(ps f | grep '[c]ount_itself.sh' | grep -v '    \\_')
echo "$nb"
sleep 20

执行:

root@myserver:/# ./count_itself.sh
17725 pts/1    S+     0:00  \_ /bin/bash ./count_itself.sh

已在bg中运行的执行:

Execution with one already running in bg :

root@myserver:/# ./count_itself.sh &
[1] 17733
root@myserver:/# ./count_itself.sh
17733 pts/1    S      0:00  \_ /bin/bash ./count_itself.sh
17739 pts/1    S+     0:00  \_ /bin/bash ./count_itself.sh

说明(据我了解):

  • ps f返回活动进程的树
  • grep '[c]ount_itself.sh'将前面的命令限制为仅显示count_itself.sh
  • 的实例
  • ps f returns the tree of active processes
  • grep '[c]ount_itself.sh' restricts the previous command to only showing instances of count_itself.sh

返回

17808 pts/1    S+     0:00  \_ /bin/bash ./count_itself.sh
17809 pts/1    S+     0:00      \_ /bin/bash ./count_itself.sh

  • grep -v ' \\_'排除包含4个空格(相当于制表符)的行,然后是\_,这与子流程相对应
    • grep -v ' \\_' excludes rows containing 4 spaces (equivalent of a tab) then \_, which correspond to subprocesses
    • 这篇关于Bash脚本错误地计数了自己的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆