Linux命令 – ‘ps’

我的目标是用高斯PID找到进程(是的,我知道可以只做ps -ef | tail -n 1,但我想首先找到PID然后找到进程),所以我用下面的命令找到了使用最高PID的过程:
 ps -ef | cut -d“” – f 6 | sort | tail -n 1
然后我发现ps -p获得最高的PID并输出匹配过程(当我手动复制PID时有效)但出于某种原因,当我把’|’他们之间说语法错误.有谁可以指出问题是什么?
如果你有更好的方法来发布这个东西.

TNX,
院长

ps,不起作用的完整命令是:
ps -ef | cut -d“” – f 6 | sort | tail -n 1 | ps -p.

解决方法:

为程序提供参数和写入程序的标准输入之间存在差异.

在第一种情况下,程序将参数列表作为字符串数组读取,可以由程序解释.在第二种情况下,程序基本上从特殊文件中读取并处理其内容.你在程序名后面放的所有内容都是参数. ps期望许多可能的参数,例如-p和进程的PID.在您的命令中,您不提供PID作为参数,而是写入ps的stdin,它忽略它.

但您可以使用xargs,它读取其标准输入并将其用作命令的参数:

ps -ef | cut -d " " -f 6 | sort | tail -n1 | xargs ps -p

这就是xargs所做的(来自man):

xargs - build and execute command lines from standard input

或者你可以使用命令替换,就像janos所示.在这种情况下,shell将$()内的表达式计算为一个命令,并改为输出输出.因此,在扩展发生后,您的命令看起来像ps -p 12345.

男子打击:

Command Substitution
   Command substitution allows the output of a command to replace the com‐
   mand name.  There are two forms:

          $(command)
   or
          `command`

   Bash performs the expansion by executing command and replacing the com‐
   mand substitution with the standard output of  the  command,  with  any
   trailing newlines deleted.  Embedded newlines are not deleted, but they
   may be removed during word splitting.  The command  substitution  $(cat
   file) can be replaced by the equivalent but faster $(< file).

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...