Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wyc9296
V2EX  ›  Linux

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

  •  1
     
  •   wyc9296 · Jun 22, 2022 · 2421 views
    This topic created in 1422 days ago, the information mentioned may be changed or developed.

    想要看所有的包含某个字符的进程的 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 replies    2022-06-22 17:46:45 +08:00
    chengxiao
        1
    chengxiao  
       Jun 22, 2022 via Android
    装 htop
    wyc9296
        2
    wyc9296  
    OP
       Jun 22, 2022
    @chengxiao 我的第一行命令是可以在成功运行交互界面的,可以达到我的目的。只是想知道,是不是所有交互界面都不能从管道获取输入吗
    hsfzxjy
        3
    hsfzxjy  
       Jun 22, 2022   ❤️ 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 {}
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3389 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 87ms · UTC 11:25 · PVG 19:25 · LAX 04:25 · JFK 07:25
    ♥ Do have faith in what you're doing.