将stdout作为命令行util的文件名传递? [英] pass stdout as file name for command line util?

查看:87
本文介绍了将stdout作为命令行util的文件名传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用命令行实用程序,该实用程序需要传递文件名以将输出写入例如.

I'm working with a command line utility that requires passing the name of a file to write output to, e.g.

foo -o output.txt

它唯一写入stdout的内容是一条消息,表明它已成功运行.我希望能够将写入output.txt的所有内容通过管道传输到另一个命令行实用程序.我的动机是output.txt最终将是一个40 GB的文件,不需要保留,我宁愿通过管道传输流,也不能以逐步的方式处理大量文件.

The only thing it writes to stdout is a message that indicates that it ran successfully. I'd like to be able to pipe everything that is written to output.txt to another command line utility. My motivation is that output.txt will end up being a 40 GB file that I don't need to keep, and I'd rather pipe the streams than work on massive files in a stepwise manner.

在这种情况下,有什么方法可以将实际输出(即output.txt)传送到另一个命令?我可以以某种方式神奇地将stdout作为文件参数传递吗?

Is there any way in this scenario to pipe the real output (i.e. output.txt) to another command? Can I somehow magically pass stdout as the file argument?

推荐答案

解决方案1:使用流程替换

最方便的方法是使用进程替换.在bash中,语法如下:

Solution 1: Using process substitution

The most convenient way of doing this is by using process substitution. In bash the syntax looks as follows:

foo -o >(other_command)

(请注意,这是 bashism .其他shell也有类似的解决方案,但最重要的是,它不是可移植的)

(Note that this is a bashism. There's similar solutions for other shells, but bottom line is that it's not portable.)

您可以按以下步骤明确/手动执行上述操作:

You can do the above explicitly / manually as follows:

  1. 使用mkfifo命令创建命名管道.

mkfifo my_buf

  • 使用该文件作为输入启动您的其他命令

  • Launch your other command with that file as input

    other_command < my_buf
    

  • 执行foo并将其输出写入my_buf

  • Execute foo and let it write it's output to my_buf

    foo -o my_buf
    

  • 解决方案3:使用/dev/stdout

    您还可以如下使用设备文件/dev/stdout

    Solution 3: Using /dev/stdout

    You can also use the device file /dev/stdout as follows

    foo -o /dev/stdout | other_command
    

    这篇关于将stdout作为命令行util的文件名传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆