我想pstree
在一组我发现使用的 pid 上运行ps
ps -aux | grep ^username | awk '{pstree $2}'
不幸的是,输出是空的,但是如果我pstree
使用相同的 pid 手动运行命令,我会得到所需的输出。命令有什么问题awk
?或者我如何通过其他方式达到预期的结果?
尝试
ps -aux | grep ^username | awk '{print $2}' | xargs pstree
(原样, pstree 是一个空变量值)
这可以归结为
ps -aux | awk '/^username/{print $2}' | xargs pstree
IHTH
使用中的system
功能awk
。你也不需要grep
这里
ps -aux | awk '$1=="username"{system("pstree $2")}'
system
像这样在 awk 中使用函数:
awk '{system("pstree " $2)}'
您可以将命令缩短为:
ps -aux | awk ' /^username/ { system("pstree " $2) }'