views:

57

answers:

3

How do u redirect a small(<20k) file to be piped to another program, so that it is never written to disc.

You could use cfront and gcc as an example. Any example will do as long as you are redirecting something that normally outputs a file to something that usually accepts a file.

You could could use any script or shell, but I would prefer to see bash or perl.

+3  A: 

interesting! I tried this:

$ mkfifo fifo.txt
$ curl http://www.google.com -o fifo.txt | wc fifo.txt

almost a pipe ... a named pipe :p

On the other hand if all you are interested in print it on stdout (without having to pipe):

$ curl http://www.google.com -o `tty`
kartheek
+1  A: 

If a program is expecting to read it's input from a file then that's pretty much what you're going to have to do. To avoid touching the file system, all the programs you are using have to understand how to read from stdin and write to stdout (or some other device that's not a file system).

Many programs in the unix universe are capable of reading from and writing to std{in,out} just as easily as they are to a file. A good example is gnu tar and gzip. It's possible for tar to write to stdout and to pipe that output directly into gzip:

tar cf - foo/ | gzip -c > foo.tgz

but that requires both tar and gzip to be able to read/write to stdin/stdout as well as regular files.

How you achieve this in your own program depends on the language involved but in most cases handling stdout & stderr is pretty much the same as any other file. Your command line arguments should allow the user to choose this as an option.

seejay
Error: broken pipe :)
TMN
Seejay if you could make larger or put in bold the lesson learned "it requires both programs be able to read/write to stdin/stdout". I will mark this as the accepted answer ;) . That is of course unless someone posts how to do it with programs that do not have the capabilities.
GlassGhost
GlassGhost, update as per your suggestion. thx
seejay
A: 

If program A is going to run program B, you can use the open function to create an anonymous pipe between A and B's STDIN:

program A.pl:

#!/usr/bin/perl

use strict;
use warnings;

my $pid = open my $pipe, "|-", "./B.pl"
    or die "could not run B.pl: $!";

for my $i ("a" .. "g") {
    print $pipe "$i\n";
}
close $pipe;

waitpid $pid, 0;

program B:

#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;
while (my $line = <>) {
    print $i++, ": $line";
}
Chas. Owens