bash 如何附加到管道上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/331224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:34:03  来源:igfitidea点击:

How do I append onto pipes?

bashpipe

提问by Daniel O

So my question is if I can somehow send data to my program and then send the same data AND its result to another program without having to create a temporary file (in my case ouputdata.txt). Preferably using linux pipes/bash.

所以我的问题是我是否可以以某种方式将数据发送到我的程序,然后将相同的数据及其结果发送到另一个程序,而无需创建临时文件(在我的情况下为 ouputdata.txt)。最好使用 linux 管道/bash。

I currently do the following:

我目前执行以下操作:

cat inputdata.txt | ./MyProg > outputdata.txt

cat inputdata.txt | ./MyProg > outputdata.txt

cat inputdata.txt outputdata.txt | ./MyProg2

cat inputdata.txt outputdata.txt | ./我的程序2

回答by derobert

Here is another way, which can be extended to put the output of two programs together:

这是另一种方法,可以扩展为将两个程序的输出放在一起:

( Prog1; Prog2; Prog3; ...  ) | ProgN

That at least works in Bash.

这至少适用于 Bash。

回答by S.Lott

Choice 1 - fix MyProgto write the merged output from the input and it's own output. Then you can do this.

选择 1 - 修复MyProg从输入和它自己的输出写入合并输出。然后你可以这样做。

./MyProg <inputdata.txt | ./MyProg2

Choice 2 - If you can't fix MyProgto write both input and output, you need to merge.

选择 2 - 如果不能MyProg同时写入输入和输出,则需要合并。

./MyProg <inputdata.txt | cat inputdata.txt - | ./MyProg2