Opening a pipe to a command 
Author Message
 Opening a pipe to a command

This maybe a dumn question (can't see the wood for the trees and all that) but
I want to open up a pipe to a command and get back all it's output. Using
system() or backticks works as long as the command isn't interactive, for
example system("rlogin machine -l user") will hand over to STDIN for the
password, but I want that to come from the perl script and still get any
output from the command back to do whatever I want with.

        What I want to do is open(PIPE,"| command |"); which of course can't
be done. Perl should be able to do this in one form or another.

        Yours, waiting to kick myself on an obvious reply,

                Steve Youell
--






Fri, 09 Feb 1996 21:23:06 GMT  
 Opening a pipe to a command

Quote:

>    What I want to do is open(PIPE,"| command |"); which of course can't
>be done. Perl should be able to do this in one form or another.

>    Yours, waiting to kick myself on an obvious reply,

check out open2.pl in /usr/local/lib/perl (or wherever your perl library
is), but be aware that it's not hard to deadlock the processes :-)

Mike
--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | Reservoir Place

Meiko tel: (617) 890 7676         | Waltham, MA 02154



Fri, 09 Feb 1996 21:57:55 GMT  
 Opening a pipe to a command

|>This maybe a dumn question (can't see the wood for the trees and all that) but
|>I want to open up a pipe to a command and get back all it's output. Using
|>system() or backticks works as long as the command isn't interactive, for
|>example system("rlogin machine -l user") will hand over to STDIN for the
|>password, but I want that to come from the perl script and still get any
|>output from the command back to do whatever I want with.
|>
|>   What I want to do is open(PIPE,"| command |"); which of course can't
|>be done. Perl should be able to do this in one form or another.

I just provided this snippet for somebody else....

---------------------------------------
pipe(INSTD, OUTSTD);
pipe(INERR, OUTERR);

if (fork) {
        close(OUTSTD);
        close(OUTERR);
        wait;
        while (<INSTD>) { print "OUT: $_\n"; }  
        while (<INERR>) { print "ERR: $_\n"; }

Quote:
} else {

        close(STDOUT);
        close(STDERR);

        open(STDOUT, ">&OUTSTD");
        select STDOUT; $| = 1;
        open(STDERR, ">&OUTERR");
        select STDERR; $| = 1;

Quote:
}

---------------------------------------

This example has been tested and works.  You should be able to alter
it to do this.....
---------------------------------------
pipe(INSTD, OUTSTD);
pipe(INERR, OUTERR);
pipe(ININ, OUTIN);

if (fork) {
        close(OUTSTD);
        close(OUTERR);
        close(ININ);
        select OUTIN; $| = 1;
        print OUTIN $input;

        wait;
        while (<INSTD>) { print "OUT: $_\n"; }  
        while (<INERR>) { print "ERR: $_\n"; }

Quote:
} else {

        close(STDOUT);
        close(STDERR);
        close(STDIN);

        open(STDOUT, ">&OUTSTD");
        select STDOUT; $| = 1;
        open(STDERR, ">&OUTERR");
        select STDERR; $| = 1;
        open(STDIN, "<&ININ");

Quote:
}

---------------------------------------

This creates pipes for stdin, stdout and stderr for the child
process.  I haven't tested this version.

Lezz "JAPH" Giles



Sat, 10 Feb 1996 04:14:54 GMT  
 Opening a pipe to a command

Quote:

>This example has been tested and works.

...but comes with the usual caveat: it can cause deadlock if you're not
careful.  (I think this question is in the FAQ.)

Michael.



Sat, 10 Feb 1996 09:02:41 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. piping command output to open()

2. open a piped command including stderr

3. open COMMAND with a pipe

4. open command that pipes both in and out

5. unix command displayed when using PIPES with open.

6. Killing a pipe-opened command in Win32?

7. Piping in stderr when creating a pipe with the open call

8. PERLFUNC: open - open a file, pipe, or descriptor

9. PERLFUNC: open - open a file, pipe, or descriptor

10. Perl's open() pukes when trying to open a read-type pipe

11. HELP: Piping shell command into perl command?

12. open(PIPE,"| command |") -- pipe & fork?

 

 
Powered by phpBB® Forum Software