Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

WWWXXXAPC, WWWXXX .CMD, Wwwwxxx reemine, WWWXXX.VCBA, WWWXXX.VCBA, TheboL.wwwxxxxx, WWWXXXAPC , wwwxxn.xsss, wwwxxxapc, WWWXXX.VCBA

Links

XODOX
Impressum

#1: sending output to STDOUT or a file

Posted on 2008-04-22 20:26:28 by vahid

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,

Report this message

#2: Re: sending output to STDOUT or a file

Posted on 2008-04-22 21:03:25 by xhoster

Vahid <vahid.moghaddasi@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

--
-------------------- 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.

Report this message

#3: Re: sending output to STDOUT or a file

Posted on 2008-04-22 21:04:15 by someone

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

Report this message

#4: Re: sending output to STDOUT or a file

Posted on 2008-04-23 06:45:01 by Charles DeRykus

On Apr 22, 11:26 am, Vahid <vahid.moghadd...@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.

Another possibility:

if ( -t STDOUT ) {
print ...
} else {
open ( local *STDOUT, '>', $LOG ) or die $!;
print STDOUT ...
}


--
Charles DeRykus

Report this message

#5: Re: sending output to STDOUT or a file

Posted on 2008-04-23 20:34:52 by Vahid Moghaddasi

On Apr 22, 3:03 pm, xhos...@gmail.com wrote:
> Vahid <vahid.moghadd...@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?

Report this message

#6: Re: sending output to STDOUT or a file

Posted on 2008-04-23 23:39:58 by xhoster

Vahid Moghaddasi <moghaddasi@gmail.com> wrote:

> >
> > 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.

Report this message