Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ cat foo
foo
bar

Now if I do:

$ some_program foo

It's working.

But if I try:

$ cat foo | some_program

It's not working.

I'm looking for a clean way to pipe input to some_program without having to use messy temporary files.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Piping will put cat foo's stdout to some_program's stdin. In this case some_program is expecting an argument, not stdin, so you'll want to store cat foo's result into a variable and then call some_program $variable.

I'm not too sure on bash scripting, but try this?

bar=`cat foo`
some_program $bar

Actually, maybe this will work...

some_program `cat foo`
share|improve this answer
    
You're the best! –  ms123 Mar 14 '12 at 10:30
    
ms123: Please edit your question if you were looking command line argumets from file instead of filename. Based on your current question this is different thing –  Cougar Mar 14 '12 at 11:15
add comment

In bash you can use process substitution

$ some_program <(cat foo)

You can add as much parameters as you like this way as long you know that these parameters will be file names that will be read only. You can't read and write at the same time.

share|improve this answer
add comment

Besides the way @Bob proposed, there is another, more flexible method: xargs:

You could use

cat foo | xargs some_program

which is basically equivalent of

some_program `cat foo`

If the program is expecting only one argument at a time, you can do

cat foo | xargs -n 1 some_program

And some_program would be called once for each input line.

There are many other options to xargs, like careful handling of "bad" filenames (with spaces and other special characters), limiting the number of simultaneously running processes etc., which could be found in man-page.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.