【问题标题】:How to display the output of a Linux command on stdout and also pipe it to another command? [duplicate]如何在标准输出上显示 Linux 命令的输出并将其通过管道传输到另一个命令? [复制]
【发布时间】:2012-04-24 08:57:14
【问题描述】:

可能重复:
How to pipe stdout while keeping it on screen ? (and not to a output file)

例如我要运行命令:

ls -l

然后我有输出到标准输出:

drwxr-xr-x 2 user user 4096 Apr 12 12:34 Desktop
-rw-rw-r-- 1 user user 1234 Apr 12 00:00 file

我想将此输出重定向到另一个命令以进行进一步处理(例如重定向到“head -1”以提取第一行)。我可以只用一行吗?

【问题讨论】:

  • tee 会做你想做的事。另外,这个问题更适合superuser.com
  • @icyrock.com 第一种方法(tee 输出到tty)对我有用。但是第二种方法(使用进程替换)没有。总之问题解决了。谢谢!
  • @Lacek 没问题,很高兴它成功了!

标签: linux shell command pipe


【解决方案1】:

是的,tee 可以。比如:

ls -l | tee | head -1

要将输出附加到文件:

ls -l | tee -a output.txt

【讨论】:

  • 谢谢,但是ls -l 的输出没有出现在控制台上。我想要的是ls -lls -l | head -1 的输出。
【解决方案2】:

有一个名为tpipe 的工具,它允许您将一个命令通过管道传递给另外两个命令(如fork),但大多数机器上默认没有安装它。使用它,您的问题将通过以下方式解决:

ls -l | tpipe head -1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 2018-08-18
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2011-06-28
    • 2020-11-02
    • 2013-10-13
    相关资源
    最近更新 更多