Often, I have found myself wishing that there were some way to force a command that requires a filename argument to accept input piped in from something else instead. Today was one of those days and I could not sit back any longer and take it. So, I wrote this quick little script that solves my problem.
filepipe.pl takes it's first argument as an alias that will
act as a place-holder that you place into the command where it would normally want a filename, the rest of the args afterwards are treated as the command and it's arguments.
eg:
./foo | ./filepipe.pl ALIAS ./bar ALIAS
would cause bar to accept the output of foo as if it were a file.
This works by supplying filepipe.pl's STDIN sysmlink in /proc/$$/fd as the file for bar to open, thus causing bar to
consume the data piped into filepipe.pl as the file.
This of course has it's limitations, it will only work if bar opens the file only for reading and doesn't check to see if it's a "normal" file.
Improvements (or a better method altogether) welcome:)
Note: A more portable way of doing this would be to use named pipes. But because opening a pipe for writing blocks until another process opens it for reading, this gets ugly.
You would have to fork and handle the reader and writer seperately. I am simply too lazy to do that when this does what I want with so little code:P
#!/usr/bin/perl
# filepipe.pl
my $fhalias = shift;
my $fh = fileno(STDIN);
my $file = "/proc/$$/fd/$fh";
system(
map {
s/$fhalias/$file/g;
$_;
} @ARGV
);