我是unix-like的新手。我遇到了一个奇怪的问题,我通过搜索找不到答案。
#!/bin/bash
me=`basename "$0"`
echo $(ps -e | grep "$me" | wc -l)
ps -e | grep "$me" | wc -l
执行完bash脚本后,回显显示2,ps只显示1这就是我想要的。怎么会发生这种情况?为什么echo给我一个额外的过程?
答案 0 :(得分:1)
正如查尔斯达菲指出的那样,std::size_t operator()(std::vector<T> const &in) const
。这回答了我的问题。显然我还有很多东西需要学习。感谢您的帮助。
答案 1 :(得分:0)
赛勒斯的评论指出;这个脚本:
me=$(basename $0)
ps -ef |grep $me
以“./ps.sh”启动时,打印:
auser@pc:/tmp$ ./ps.sh
auser 4425 4422 0 08:42 pts/3 00:00:00 grep ps.sh
auser@pc:/tmp$
这里没有涉及子shell,它是由ps(1)列出的grep(1)本身。使用“bash ps.sh”输出的相同脚本输出:
auser 4426 3946 0 08:44 pts/3 00:00:00 bash ps.sh
auser 4429 4426 0 08:44 pts/3 00:00:00 grep ps.sh
这是OP得到的结果,即使没有子弹。更明确的是:
auser@pc:/tmp$ ps -ef |grep grep
auser 4467 3946 0 08:49 pts/3 00:00:00 grep grep
答案 2 :(得分:0)
尽管您正在使用$()
创建子外壳,但是也可以使用grep -v grep
将其复制。
所以:
$(ps -e | grep "$me" | grep -v grep | wc -l)
将返回1而不是2