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 );
  • Comment on Linux: force commands that expect filename args to accept piped input
  • Download Code

Replies are listed 'Best First'.
•Re: Linux: force commands that expect filename args to accept piped input
by merlyn (Sage) on Feb 24, 2002 at 16:02 UTC
    If I'm not mistaken, any system that supports /proc/$$/fd/0 also supports /dev/fd/0, so you could just learn to type that instead of carrying your Perl program around. Example:
    complex pipe | of commands | nastycommand file1 file2 /dev/fd/0 file3

    -- Randal L. Schwartz, Perl hacker

      Thanks! I knew there must have been a simpler solution escaping me.

      -- O thievish Night, Why should'st thou, but for some felonious end, In thy dark lantern thus close up the stars? --Milton
Re: Linux: force commands that expect filename args to accept piped input
by davebailey (Initiate) on Sep 06, 2008 at 13:56 UTC
    or, you could use xargs