2013-10-29 45 views
0

如果我编写ps -ef,则返回当前运行的所有进程。如果我输入ps -ef | grep xxx,那么它将返回所有使用子字符串xxx运行的进程。但ps -ef | grep xxx也是当前进程,因此它也会在列表中返回grep xxx除当前进程外正在运行的进程

我只是想从列表中消除grep xxx

任何人都可以帮助我。
感谢

回答

2

尝试这样做:

pgrep -fl xxx 

另一种解决方案

ps -ef | grep '[x]xx' 

这是一个简单的正则表达式技巧来避免重复

pgrep封装在procps,在Debian:

$ LANG=C apt-cache show procps 
Package: procps 
Priority: important 
Section: admin 
Installed-Size: 760 
Maintainer: Craig Small <[email protected]> 
Replaces: bsdutils (<< 2.9x-1), watch 
Provides: watch 
Depends: libc6 (>= 2.3.4), libncurses5 (>= 5.7+20100313), libncursesw5 (>= 5.7+20100313), lsb-base (>= 3.0-10), initscripts 
Recommends: psmisc 
Conflicts: libproc-dev (<< 1:1.2.6-2), pgrep (<< 3.3-5), procps-nonfree, w-bassman (<< 1.0-3), watch 
Size: 249178 
Description: /proc file system utilities 
This package provides command line and full screen utilities for browsing 
procfs, a "pseudo" file system dynamically generated by the kernel to 
provide information about the status of entries in its process table 
(such as whether the process is running, stopped, or a "zombie"). 
. 
It contains free, kill, pkill, pgrep, pmap, ps, pwdx, skill, slabtop, 
snice, sysctl, tload, top, uptime, vmstat, w, and watch. 
Homepage: http://procps.sf.net/ 
Tag: admin::monitoring, interface::commandline, interface::text-mode, role::program, scope::utility, uitoolkit::ncurses, use::monitor, works-with::software:running 
+0

我喜欢用'Another解决方案'更新:) – devnull

+0

同样认为@同时。我在5分钟之前回答了你,并且我在管道中添加了这个片段。 –

+0

是的,因为当时我正在回答我知道。我相信你在管道中有其他片段。祝你好运! – devnull

1

与其说

ps -ef | grep xxx 

的说

ps -ef | grep [x]xx 

,你不会看到grep xxx输出。 (基本上把所需单词的第一个字符作为字符类别[]。)