Click here to get back home

sending output to STDOUT or a file

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sending output to STDOUT or a file Vahid 04-22-2008
Posted by Vahid on April 22, 2008, 2:26 pm
Please log in for more thread options
Hi,
I have a program that generates some output but I need it to dump them
to a file if ran in a background and print on screen if ran in
foreground. This is a sample program that need some help with:
#!/bin/perl
#
use warnings;
use strict;
#
my $today=`date +%Y%m%d`; chomp $today;
my $LOG="/tmp/output.$today.log";
my $foreground="$LOG";
if ( -t STDOUT) {
$foreground=STDOUT;
}
open (LOGfh, "> $foreground") or die "ERROR: $!";

print LOGfh "The foreground is $foreground\n";
close LOGfh;

Of course this will give me error for using STDOUT in open.
Thanks,

Posted by xhoster on April 22, 2008, 3:03 pm
Please log in for more thread options
> Hi,
> I have a program that generates some output but I need it to dump them
> to a file if ran in a background and print on screen if ran in
> foreground. This is a sample program that need some help with:
> #!/bin/perl
> #
> use warnings;
> use strict;
> #
> my $today=`date +%Y%m%d`; chomp $today;
> my $LOG="/tmp/output.$today.log";
> my $foreground="$LOG";
> if ( -t STDOUT) {
> $foreground=STDOUT;
> }
> open (LOGfh, "> $foreground") or die "ERROR: $!";
>
> print LOGfh "The foreground is $foreground\n";
> close LOGfh;
>
> Of course this will give me error for using STDOUT in open.
> Thanks,

Maybe I'm missing something, but I think this does it:

unless ( -t STDOUT) {
my $today=`date +%Y%m%d`; chomp $today;
open STDOUT, ">/tmp/output.$today.log" or die $!;
};

print "The foreground is $foreground\n";

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Vahid Moghaddasi on April 23, 2008, 2:34 pm
Please log in for more thread options
On Apr 22, 3:03 pm, xhos...@gmail.com wrote:
> > Hi,
> > I have a program that generates some output but I need it to dump them
> > to a file if ran in a background and print on screen if ran in
> > foreground. This is a sample program that need some help with:
> > #!/bin/perl
> > #
> > use warnings;
> > use strict;
> > #
> > my $today=`date +%Y%m%d`; chomp $today;
> > my $LOG="/tmp/output.$today.log";
> > my $foreground="$LOG";
> > if ( -t STDOUT) {
> > $foreground=STDOUT;
> > }
> > open (LOGfh, "> $foreground") or die "ERROR: $!";
>
> > print LOGfh "The foreground is $foreground\n";
> > close LOGfh;
>
> > Of course this will give me error for using STDOUT in open.
> > Thanks,
>
> Maybe I'm missing something, but I think this does it:
>
> unless ( -t STDOUT) {
> my $today=`date +%Y%m%d`; chomp $today;
> open STDOUT, ">/tmp/output.$today.log" or die $!;
>
> };
>
> print "The foreground is $foreground\n";
>
> Xho
>
Yes that does the trick, thank you.
I don't understand how does the output of the last print statement
goes to STDOUT if the program is running in the background?


Posted by xhoster on April 23, 2008, 5:39 pm
Please log in for more thread options

> >
> > Maybe I'm missing something, but I think this does it:
> >
> > unless ( -t STDOUT) {
> > my $today=`date +%Y%m%d`; chomp $today;
> > open STDOUT, ">/tmp/output.$today.log" or die $!;
> >
> > };
> >
> > print "The foreground is $foreground\n";
> >
> > Xho
> >
> Yes that does the trick, thank you.
> I don't understand how does the output of the last print statement
> goes to STDOUT if the program is running in the background?

Sorry, I don't understand the question. Maybe you are confusing
STDOUT with the terminal. Often STDOUT is hooked up to the terminal,
but sometimes it is not.

The concept of "background" is somewhat fuzzy. In Linux, if you start a
program "in the background" by adding a &, that doesn't change the STDOUT
of the program. For example, the below prints "1" because the program's
STDOUT is still the terminal, despite being run in the background.

perl -le 'sleep 3; print -t STDOUT' &

Where as this doesn't print "1", because it's STDOUT is not the terminal,
even though it is running in the foreground;

perl -le 'warn -t STDOUT' > /dev/null

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by John W. Krahn on April 22, 2008, 3:04 pm
Please log in for more thread options
Vahid wrote:
> Hi,
> I have a program that generates some output but I need it to dump them
> to a file if ran in a background and print on screen if ran in
> foreground. This is a sample program that need some help with:
> #!/bin/perl
> #
> use warnings;
> use strict;
> #
> my $today=`date +%Y%m%d`; chomp $today;

use POSIX 'strftime';

my $today = strftime '%Y%m%d', localtime;


> my $LOG="/tmp/output.$today.log";
> my $foreground="$LOG";

my $foreground = $LOG;

perldoc -q quoting


> if ( -t STDOUT) {
> $foreground=STDOUT;
> }
> open (LOGfh, "> $foreground") or die "ERROR: $!";
>
> print LOGfh "The foreground is $foreground\n";
> close LOGfh;
>
> Of course this will give me error for using STDOUT in open.

perldoc -q "How can I use a filehandle indirectly"
perldoc -q "How do I make an array of filehandles"
perldoc -f open
perldoc perlopentut



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Similar ThreadsPosted
Net::SSH::Perl sending output to STDOUT January 26, 2005, 11:45 am
Sending a binary file through a script (binmode/stdout/mime type) December 13, 2004, 1:50 pm
Using SSH and Expect to return STDOUT output from a C executable May 9, 2007, 11:22 am
I want to output all of my STDOUT to a single line....dont want to scroll September 11, 2006, 3:37 pm
Sending an incomplete file? March 10, 2007, 1:05 am
Sending a pdf file with perl August 29, 2007, 10:42 pm
copy stdout fails with permission denied when stdout is redirected December 7, 2005, 1:15 pm
Redirecting STDOUT to a file April 25, 2006, 12:53 pm
Printing to a file Or STDOUT August 21, 2006, 4:35 am
sending file handle as function argument June 8, 2005, 6:40 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap