“ps -aef | grep $(pwd)”命令是什么意思?

Rak*_* KR 5 command-line grep ps

这个命令的含义是什么,它有什么作用?

ps -aef | grep `pwd`
Run Code Online (Sandbox Code Playgroud)

saj*_*i89 8

ps的手册页:

   -a              Select all processes except both session leaders (see getsid(2)) and
                   processes not associated with a terminal.

   -f              Do full-format listing. This option can be combined
                   with many other UNIX-style options to add additional
                   columns. It also causes the command arguments to be
                   printed. When used with -L, the NLWP (number of
                   threads) and LWP (thread ID) columns will be added. See
                   the c option, the format keyword args, and the format
                   keyword comm.
   -e              Select all processes. Identical to -A.
Run Code Online (Sandbox Code Playgroud)

grep用于print lines matching a pattern.

它能做什么

命令

ps -aef | grep `pwd`
Run Code Online (Sandbox Code Playgroud)

打印出匹配命令的输出的所有行pwd(这将是该路径当前工作目录),从输出ps -aef

例如:

saji@geeklap:~$ pwd
/home/saji

saji@geeklap:~$ ps -aef | grep `pwd`
saji      2854  2814  0 09:51 ?        00:00:00 /usr/bin/ssh-agent /usr/bin/gpg-agent --daemon --sh --write-env-file=/home/saji/.gnupg/gpg-agent-info-geeklap /usr/bin/dbus-launch --exit-with-session gnome-session --session=ubuntu
saji      2855  2814  0 09:51 ?        00:00:00 /usr/bin/gpg-agent --daemon --sh --write-env-file=/home/saji/.gnupg/gpg-agent-info-geeklap /usr/bin/dbus-launch --exit-with-session gnome-session --session=ubuntu
saji      2879     1  0 09:51 ?        00:00:00 /usr/lib/gvfs//gvfs-fuse-daemon -f /home/saji/.gvfs
saji     14242 14148  0 15:26 pts/7    00:00:00 grep --color=auto /home/saji
Run Code Online (Sandbox Code Playgroud)

如您所见,输出显示了与我当前工作目录匹配的行,即/home/saji.

背景信息:
如果命令在 $(...) 或 中...,则运行该命令并捕获输出(打印到屏幕上的内容)并替换为原始 $() 或 `` 字符串所在的位置。所以实际运行的命令是 grep pwd。

有关更多信息,请参阅此链接。(感谢@minerz029提供此信息)。

请查看以下链接以获取手册页本身的详细技术答案:

http://explainshell.com/explain?cmd=ps+-aef+|+grep+%60pwd%60