V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wyc9296
V2EX  ›  Linux

top 的交互界面不能从管道获取输入吗

  •  1
     
  •   wyc9296 · 2022-06-22 16:23:19 +08:00 · 1452 次点击
    这是一个创建于 663 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想要看所有的包含某个字符的进程的 top 结果:

    top -c -p $(ps aux|grep -i some_string|perl -ae 'print "$F[1],"'|perl -ne 's/,$//; print') # 可以运行 OK
    
    ps aux|grep -i some_string|perl -ae 'print "$F[1],"'|perl -ne 's/,$//; print'|xargs -I {} top -c -p {} # 报错 top: failed tty get
    

    而 top 的非交互界面却可以接收管道输入,并正常得到结果:

    ps aux|grep -i some_string|perl -ae 'print "$F[1],"'|perl -ne 's/,$//; print'|xargs -I {} top -b -n 1 -c -p {} # 可以运行 OK
    

    请各位彦祖帮忙看下为什么。谢谢

    3 条回复    2022-06-22 17:46:45 +08:00
    chengxiao
        1
    chengxiao  
       2022-06-22 16:48:31 +08:00 via Android
    装 htop
    wyc9296
        2
    wyc9296  
    OP
       2022-06-22 16:56:32 +08:00
    @chengxiao 我的第一行命令是可以在成功运行交互界面的,可以达到我的目的。只是想知道,是不是所有交互界面都不能从管道获取输入吗
    hsfzxjy
        3
    hsfzxjy  
       2022-06-22 17:46:45 +08:00   ❤️ 1
    交互式命令通常要求 stdout 是 tty ,但管道会让 stdout 失去 tty 这一属性。你需要的是 process substitution

    xargs -I {} -a <(ps aux | grep -i some_string | perl -ae 'print"$F[1],"' | perl -ne 's/,$//; print') top -c -p {}
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3907 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:17 · PVG 12:17 · LAX 21:17 · JFK 00:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.