我的目标是用高斯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).