2

Hypothetical situation. I have a command line program in *nix (linux, BSD, etc.). It was written so that you pass it a text file as an argument

$ program file.txt 

Run the program, it looks at the text in file.txt.

Is it possible to "trick" this program into accepting input from a file stream rather than reading a file via disk? I'm pretty comfortable using unix pipes to do stuff, but there's still something a little mysterious about their internals that make it so I can't say (definitively) yes or not to the above question.

4 Answers 4

4

bash lets you do this:

program <(otherprogram) 

This uses the output of otherprogram as the contents of a file passed to program.

Sign up to request clarification or add additional context in comments.

5 Comments

That's pretty neat. What's it called, and is is possible to use if hte program accepts TWO different file paths? (something like diff)
Process substitution, and yes, you can use... up to 60 or so I think.
Sweet (more info here for the curious tldp.org/LDP/abs/html/process-sub.html)
Piping a file into a process's stdin is not the same as a process taking a filename and explicitly opening that file.
@Ether: Try again. This doesn't pipe it into stdin.
3

You may be interested in named pipes:

mkfifo myPipe program myPipe & anotherProgram > myPipe 

is equivalent to:

anotherProgram | program 

Comments

1

if your program is not coded to accept standard input, it won't work even if you use named pipes or process substitution

1 Comment

I think 1ch1gO is saying that using a named pipe will only work if the program is written to work on a stream. You can pass a named pipe as an argument, but if the program tries to seek it will very likely fail. Often, programmers are sloppy and do not check if a seek failed, so the program may fail in unusual ways.
0

In addition to the shell-managed trickery of the other answers:

Some unix systems have the special files /dev/stdin, and you can run e.g.

otherprogram | program /dev/stdin 

Others (e.g. linux) may have /proc/self/fd/0 which may be used the same way.

Both of these will fail if stdin is closed before the file on the command line is opened, but this will be an extremely rare occurrence. Much more likely will be it failing because the program expects to seek() the file, which does not work on pipes.

If your shell is zsh, you have another option.

program =(otherprogram) 

Which will do all the bother of setting up a temporary input file and removing it after program terminates. This will work with seek(), but may temporarily take more space.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.