views:

244

answers:

2

I'm trying to use the Unix command paste, which is like a column-appending form of cat, and came across a puzzle I've never known how to solve in Unix.

How can you use the outputs of two different programs as the input for another program (without using temporary files)?

Ideally, I'd do this (without using temporary files):

./progA > tmpA; ./progB > tmpB; paste tmpA tmpB

This seems to come up relatively frequently for me, but I can't figure out how to use the output from two different programs (progA and progB) as input to another without using temporary files (tmpA and tmpB).

For commands like paste, simply using paste $(./progA) $(./progB) (in bash notation) won't do the trick, because it can read from files or stdin.

The reason I'm wary of the temporary files is that I don't want to have jobs running in parallel to cause problems by using the same file; ensuring a unique file name is sometimes difficult.

I'm currently using bash, but would be curious to see solutions for any Unix shell.

And most importantly, am I even approaching the problem in the correct way?


Cheers!

+8  A: 

You do not need temp files under bash, try this:

paste <(./progA) <(./progB)

See "Process Substitution" in the bash manual.

Pozsár Balázs
+1  A: 

Use named pipes (FIFOs) like this:

mkfifo fA
mkfifo fB
progA > fA &
progB > fB &
paste fA fB
rm fA fB

The process substitution for Bash does a similar thing transparently, so use this only if you have a different shell.

Cristian Ciupitu
Actually, Bash uses anonymous pipes and substitutes `/dev/fd/##` on platforms that support it -- it doesn't create temporary named pipes on Linux.
ephemient