Duplicate stdout, connect it to two different commands, collect results from both to stdin of the final program

Let's say I have three programs: a generator that produces input to the processor and a verifier that can check if the processor is received correctly (both files are needed for this).

What am I doing now:

generator> in.txt && processor <in.txt> out.txt && cat in.txt out.txt | Verifier

Is it possible to achieve the same result without using explicit files? I read about duplicating input using tee and process substitution, but I did not find a way to collect both streams into one for the last step.

+7
source share
3 answers

If you do not want to create real files on your slow hard drive, you can use FIFO (First In First Out), which are also called named, because of their behavior.

mkfifo myfifo generator | tee myfifo | processor | verifier myfifo 

This is a stream of generated content in tee that duplicates it to myfifo and stdout , which is passed through processor to verifier . And the verifier also gets the stream from myfifo .

+2
source

I have not tested this, but I will try:

 { generator | tee /dev/stderr | processor ; } 2>&1 | verifier 

This will redirect the copy of generator to stderr . Then run processor on stdout in generator . Then combine both and the handset into a verifier .

However, this cannot guarantee the order in which the lines from the generator and processor reach the verifier.


Alternatively, you can try replacing the processes as shown below:

 ( generator | tee >(processor) ) | verifier 
+2
source

If you can change the processor code for stdin serial output, then stdout single line command can be simple:

 generator | processor | verifier 

Otherwise, you can use this

 generator | tee in.txt | processor | verifier in.txt 

The tee command duplicates stdout and redirects one stream to in.txt and the other to the next. In this case, you need to change the verifier to read the processor output from stdin , and the file generated by the generator is passed as a parameter.

I know that you are looking for a method to duplicate and redirect stdin to stdout , but I don’t know about it and honestly I don’t think it is possible

+1
source

All Articles