writing to terminal even with STDOUT and STDERR redirected 
Author Message
 writing to terminal even with STDOUT and STDERR redirected

I have a first perl-program ("first.pl") that calls a second perl-
program ("second.pl") and captures STDOUT and STDERR.
Is there a way inside "second.pl" to magically open a filehandle to
the terminal, even though STDOUT and STDERR are both redirected ?
I'm working under Ubuntu Linux.

first.pl
======
use strict;
use warnings;
system 'perl second.pl >file1.txt 2>file2.txt';

second.pl
======
use strict;
use warnings;
print STDOUT "This is STDOUT\n";
print STDERR "This is STDERR\n";
open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
terminal because $!";
print {$fh} "This is magically going to terminal\n";
close $fh;



Tue, 10 Jul 2012 18:31:46 GMT  
 writing to terminal even with STDOUT and STDERR redirected

Quote:

> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR. Is there a way
> inside "second.pl" to magically open a filehandle to the terminal, even
> though STDOUT and STDERR are both redirected ? I'm working under Ubuntu
> Linux.

> first.pl
> ======
> use strict;
> use warnings;
> system 'perl second.pl >file1.txt 2>file2.txt';

> second.pl
> ======
> use strict;
> use warnings;
> print STDOUT "This is STDOUT\n";
> print STDERR "This is STDERR\n";
> open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> terminal because $!";
> print {$fh} "This is magically going to terminal\n"; close $fh;

open my$fh, ">", "/dev/tty" or die ...

M4



Tue, 10 Jul 2012 21:49:06 GMT  
 writing to terminal even with STDOUT and STDERR redirected

Quote:
> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR.
> Is there a way inside "second.pl" to magically open a filehandle to
> the terminal, even though STDOUT and STDERR are both redirected ?
> I'm working under Ubuntu Linux.

> first.pl
> ======
> use strict;
> use warnings;
> system 'perl second.pl >file1.txt 2>file2.txt';

> second.pl
> ======
> use strict;
> use warnings;
> print STDOUT "This is STDOUT\n";
> print STDERR "This is STDERR\n";
> open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> terminal because $!";
> print {$fh} "This is magically going to terminal\n";
> close $fh;

The solution is probably platform-specific. Since you're on Linux,
you can use the "tty" command:

my $tty = `tty`;
chomp $tty;
print STDOUT "This is STDOUT.\n";
print STDERR "This is STDERR.\n";
open my $fh, '>', $tty or die ("open failure: $!");
print $fh "Magic terminal says 'Hello.'\n";
close $fh;



Tue, 10 Jul 2012 23:20:35 GMT  
 writing to terminal even with STDOUT and STDERR redirected

Quote:
> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR.
> Is there a way inside "second.pl" to magically open a filehandle to
> the terminal, even though STDOUT and STDERR are both redirected ?

  perldoc Term::ReadLine;

Quote:
> I'm working under Ubuntu Linux.

Does not matter...

Hope this helps,
Ilya



Wed, 11 Jul 2012 01:27:53 GMT  
 writing to terminal even with STDOUT and STDERR redirected

Quote:
> I have a first perl-program ("first.pl") that calls a second perl-
> program ("second.pl") and captures STDOUT and STDERR.
> Is there a way inside "second.pl" to magically open a filehandle to
> the terminal, even though STDOUT and STDERR are both redirected ?
> I'm working under Ubuntu Linux.

> first.pl
> ======
> use strict;
> use warnings;
> system 'perl second.pl >file1.txt 2>file2.txt';

> second.pl
> ======
> use strict;
> use warnings;
> print STDOUT "This is STDOUT\n";
> print STDERR "This is STDERR\n";
> open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> terminal because $!";
> print {$fh} "This is magically going to terminal\n";
> close $fh;

A convoluted but more Unix-y portable option
is to dup STDOUT and undo the close-on-exec
flag before the system call and then pass
the dup'ed fd in:

first.pl
========
use strict;
use warnings;
use Fcntl qw/F_SETFD/;

open( SAVEOUT, ">&STDOUT" ) or die $!;
fcntl(  SAVEOUT, F_SETFD, 0 )
  or die "Can't clear close-on-exec flag on SAVEOUT",": $!";

system "perl second.pl " . fileno(SAVEOUT) . " "
          . fileno(SAVEOUT) . " >&2"
          . " >file1.txt 2>file2.txt";

second.txt
==========
use strict;
use warnings;

my $savefd = shift;  # get STDOUT dup fd

print STDOUT "This is STDOUT\n";
print STDERR "This is STDERR\n";

open(SAVEOUT, "+<&=$savefd") or die "can't re-open SAVEOUT: $!";
print SAVEOUT "This is magically going to terminal\n";

--
Charles DeRykus



Wed, 11 Jul 2012 01:46:42 GMT  
 writing to terminal even with STDOUT and STDERR redirected


Quote:

> > I have a first perl-program ("first.pl") that calls a second perl-
> > program ("second.pl") and captures STDOUT and STDERR.
> > Is there a way inside "second.pl" to magically open a filehandle to
> > the terminal, even though STDOUT and STDERR are both redirected ?
> > I'm working under Ubuntu Linux.

> > first.pl
> > ======
> > use strict;
> > use warnings;
> > system 'perl second.pl >file1.txt 2>file2.txt';

> > second.pl
> > ======
> > use strict;
> > use warnings;
> > print STDOUT "This is STDOUT\n";
> > print STDERR "This is STDERR\n";
> > open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
> > terminal because $!";
> > print {$fh} "This is magically going to terminal\n";
> > close $fh;

> A convoluted but more Unix-y portable option

Since /dev/tty is one of only four files mentioned by name in POSIX,
it's probably safe to rely on it being there :).

Ben



Wed, 11 Jul 2012 02:13:15 GMT  
 writing to terminal even with STDOUT and STDERR redirected

Quote:

> ...

> Since /dev/tty is one of only four files mentioned by name in POSIX,
> it's probably safe to rely on it being there :).

<off-topic>

Suprisingly, I didn't find it when I was logged into FreeBSD.
which led me down the path of convolution.  Of course, it's
a free FreeBSD shell account...maybe that's significant.

</off-topic>

--
Charles DeRykus



Wed, 11 Jul 2012 04:17:21 GMT  
 writing to terminal even with STDOUT and STDERR redirected

Quote:

> > Is there a way inside "second.pl" to magically open a filehandle to
> > the terminal, even though STDOUT and STDERR are both redirected ?
> > I'm working under Ubuntu Linux.


On 22 jan, 23:20, "Mumia W." <paduille.4061.mumia.w



Thanks to everyone who replied, there are a lot of good suggestions I
can go through for my problem.



Thu, 12 Jul 2012 10:28:05 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Redirecting STDERR to file then back to terminal?

2. HELP - with redirect of STDERR and STDOUT

3. redirect STDERR to STDOUT ?????

4. redirecting stderr and stdout

5. assigning STDERR whild redirecting STDOUT to a file

6. Problem with redirecting STDOUT and STDERR

7. How to redirect STDOUT,STDERR and keep the message order

8. Redirect stderr to stdout w/ Perl under Unix?

9. Redirecting STDERR to STDOUT (on NT)

10. HELP - with redirect of STDERR and STDOUT

11. need to redirect STDOUT and STDERR

12. Redirecting stdout/stderr from a call to system()

 

 
Powered by phpBB® Forum Software