Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-17-2011, 04:10 AM   #1
LQ Newbie
Registered: Aug 2011
Posts: 18

Rep: Reputation: Disabled
How can I redirect stdout of 2 apps to a third app?

Hi guys,
I would like to redirect 2 utilities' output ( foo1.out and foo2.out) to a third utility (foo3.out). How can it be done?

I know how to redirect only one at a time:
./foo1.out | foo3.out
./foo2.out | foo3.out

Old 08-17-2011, 04:31 AM   #2
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
My first guess is "named pipes". Read on mkfifo, then play around with this quick snippet and see if you can understand what I mean:

$ # run this on one terminal emulator
$ mkfifo myfifo
$ tail -f myfifo
$ # then run this into another one, put them side by side
$ for i in $(seq 1 10); do sleep 1s; echo "loopA: $i" > myfifo; done& for j in $(seq 100 110); do sleep 1s; echo "loopB: $j" > myfifo; done&

This launches two loops on the background (so they run kind-of concurrently for our purpose), they both write to a named fifo. Then we read from that fifo on the other terminal using tail -f (but it could very well be your third tool instead).
Old 08-17-2011, 08:03 AM   #3
LQ Newbie
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Cool. I'll look in to it.
Old 08-17-2011, 09:19 AM   #4
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Probably something like this:

mkfifo myfifo
./foo1.out > myfifo &
./foo2.out > myfifo &
./foo3.out < myfifo
EDIT: Here's an example:

$ mkfifo myfifo
$ echo test1 > myfifo &
[1] 10610
$ echo test2 > myfifo &
[2] 10611
$ cat < myfifo
[1]-  Done                    echo test1 > myfifo
[2]+  Done                    echo test2 > myfifo
$ rm myfifo

Last edited by MTK358; 08-17-2011 at 09:29 AM.
Old 08-17-2011, 11:17 AM   #5
LQ Newbie
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Great. i Copy&paste. Changed the name of the utilities. And it works perfect!!

Thanks you.
Old 08-17-2011, 11:29 PM   #6
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 2,967

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Simpler just to group the commands with curly brackets:
{ ./foo1.out; ./foo2.out; } | ./foo3.out


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect AND print stdout samel_tvom Programming 12 10-07-2012 06:37 PM
Cannot redirect all stdout and stderr into file hawkgao Programming 2 05-12-2009 10:14 AM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-04-2008 12:20 AM
redirect stdout of os.system()? Chowroc Programming 7 03-02-2006 11:56 PM
redirect stdout to a varible Furlinastis Programming 3 12-07-2005 07:01 AM

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration