【问题标题】:How to reference the output of the previous command twice in Linux command?如何在 Linux 命令中两次引用上一个命令的输出?
【发布时间】:2015-04-01 21:06:29
【问题描述】:

例如,如果我想引用上一个命令的输出一次,我可以使用以下命令:

ls *.txt | xargs -I % ls -l %

但是如何引用输出两次呢?就像我怎样才能实现类似的东西:

ls *.txt | xargs -I % 'some command' % > %

PS:我知道如何在 shell 脚本中做到这一点,但我只是想要一种更简单的方法。

【问题讨论】:

    标签: linux shell command


    【解决方案1】:

    您可以将此参数传递给bash -c

    ls *.txt | xargs -I % bash -c 'ls -l "$1" > "out.$1"' - %
    

    【讨论】:

      【解决方案2】:

      您可以在 SO 上查找“tpipe”;它还会引导您“小便”(这在互联网上的其他地方不是一个好的搜索词)。基本上,它们是tee 命令的变体,它写入多个进程而不是像tee 命令那样写入文件。

      但是,对于 Bash,您可以使用 Process Substitution:

      ls *.txt | tee >(cmd1) >(cmd2)
      

      这会将tee 的输入写入cmd1cmd2 的每个命令。

      您至少可以通过两种不同的方式来安排丢失标准输出:

      ls *.txt | tee >(cmd1) >(cmd2) >/dev/null
      ls *.txt | tee >(cmd1) | cmd2
      

      【讨论】:

        猜你喜欢
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多