Directing stderr and stdout to a pipe 
Author Message
 Directing stderr and stdout to a pipe

I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
direct stderr and stdout to this file handle.  Is there any way to do
this?

I'm going to be doing a couple system() commands and I want to be able
to capture absolutely everything that comes out of them.

Thanks in advance!

Tom

--

Disclaimer:  I do not speak for Mentor Graphics.
             I can't even do the accent.



Tue, 24 Oct 1995 05:48:17 GMT  
 Directing stderr and stdout to a pipe
Tom> I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
Tom> direct stderr and stdout to this file handle.  Is there any way to do
Tom> this?

Simple.  Just use /bin/sh syntax:

        open(MAIL,"/usr/ucb/mail -s status root 2>&1 |");

Or, you could do it the hard way, if you didn't know /bin/sh very well:

        unless(open(MAIL,"-|")) { # child does:
                open(STDERR,">&STDOUT");
                exec "/usr/ucb/mail -s status root";
                die "The mailman is dead";
        }

This also allows you to specify the args exactly, avoiding shell
quoting headaches:

        $mysub = q#You can't go "home", unless you set $HOME!#;
        unless(open(MAIL,"-|")) { # child does:
                open(STDERR,">&STDOUT");
                exec "/usr/ucb/mail", "-s", $mysub, "root";
                die "The mailman is dead";
        }

$x = time; open($x,"|cat"); print $x "Just another Perl hacker,"
--
Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095

Quote: "Welcome to Portland, Oregon, home of the California Raisins!"



Tue, 24 Oct 1995 18:07:38 GMT  
 Directing stderr and stdout to a pipe

Quote:

>I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
>direct stderr and stdout to this file handle.  Is there any way to do
>this?

Since opening a pipe like starts a Bourne shell, you can (read: should be
able to) do the following:

open(MAIL,"/usr/ucb/mail -s status root 2>&1 |")

Quote:
>Thanks in advance!

Hope this helps
                                                Dabe
--






Wed, 25 Oct 1995 02:19:01 GMT  
 Directing stderr and stdout to a pipe
: I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
: direct stderr and stdout to this file handle.  Is there any way to do
: this?

It'll just call Bourne shell to interpret metacharacters, so you can say

    open(MAIL, "/usr/ucb/mail -s status root 2>&1 |")

Alternately, to avoid the shell you can say

    $child = open(MAIL, "-|");                # Fork a pipe from a subprocess.
    die "phooey on phinicky phork" unless defined $child;
    if (!$child) {
        open(STDERR, ">&STDOUT");      # That's civilized talk for 2>&1
        exec "/usr/ucb/mail -s status root";
        die "exec didn't exec";
    }

Typically it's not worth writing all that for the occasional pipe.

Larry



Wed, 25 Oct 1995 03:50:33 GMT  
 Directing stderr and stdout to a pipe

Quote:
>I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
>direct stderr and stdout to this file handle.  Is there any way to do
>this?
>I'm going to be doing a couple system() commands and I want to be able
>to capture absolutely everything that comes out of them.

Ooops, I meant :
        open(MAIL, "|/usr/ucb/mail -s status root")

*blush*

Tom

--

Disclaimer:  I do not speak for Mentor Graphics.
             I can't even do the accent.



Wed, 25 Oct 1995 05:56:08 GMT  
 Directing stderr and stdout to a pipe

Quote:


>>I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
>>direct stderr and stdout to this file handle.  Is there any way to do
>>this?

>>I'm going to be doing a couple system() commands and I want to be able
>>to capture absolutely everything that comes out of them.

>Ooops, I meant :
>    open(MAIL, "|/usr/ucb/mail -s status root")

So you want to pipe the system() calls through /usr/ucb/mail?
My quickest-n-dirtiest solution would be to include that as part of
your system call, tho that's kinda kludgey, I admit.

system('echo "Hi!" 2>&1 | /usr/ucb/mail -s status root');

again: kludgey, but it should work.
                                                Dabe
--






Thu, 26 Oct 1995 02:18:20 GMT  
 Directing stderr and stdout to a pipe
: Ooops, I meant :
:       open(MAIL, "|/usr/ucb/mail -s status root")

In that case, the safest way is

        open(MAIL, "|/usr/ucb/mail -s status root >/tmp/foo$$ 2>&1");
        print MAIL $blah;
        close MAIL;
        open(RESULT, "/tmp/foo$$");
        while (<RESULT>) {
            print "Oops: ", $_;
        }

You could also fiddle with open2.pl, and you can probably use 2>&1 in
conjunction with it.  But you risk deadlock that way.

Larry



Thu, 26 Oct 1995 03:45:34 GMT  
 Directing stderr and stdout to a pipe

   I would like to open(MAIL, "/usr/ucb/mail -s status root|") and then
   direct stderr and stdout to this file handle.  Is there any way to do
   this?

If you mean that you want to log all the actions of your perl script into
the MAIL filehandle then you should do

   open (STDOUT, "|/usr/ucb/mail -s status root") || die "open ...: $!\n";
   open (STDERR, '>&STDOUT') || die "open ...: $!\n";

If you mean that you want both to read the output of the above and write
to this command (two way pipe) (which I doubt) then look for open2() by
Tom Christiansen.

   I'm going to be doing a couple system() commands and I want to be able
   to capture absolutely everything that comes out of them.

The first solution should do it.

   Thanks in advance!

   Tom

Cheers,

--Amos
--
--Amos Shapira (Jumper Extraordinaire) |  "It is true that power corrupts,
C.S. System Group, Hebrew University,  |   but absolute power is better!"
Jerusalem 91904, ISRAEL                |



Fri, 27 Oct 1995 03:55:08 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Directing stderr when opening a pipe?

2. How do you direct STDERR into a pipe?

3. reading stdout/stderr using pipes

4. Distinguishing stdout and stderr on a pipe

5. sending stdout and stderr to a pipe in perl

6. pipe, STDOUT, and STDERR

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

8. Re-directing stderr

9. directing STDOUT back to the display

10. Newbie: How to direct STDOUT & STDERR to file AND terminal

11. Re-directing dos stderr stdout to file. How?

12. eval and trapping stdout/stderr

 

 
Powered by phpBB® Forum Software