sending output to STDOUT or a file

sending output to STDOUT or a file

am 22.04.2008 20:26:28 von 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,

Re: sending output to STDOUT or a file

am 22.04.2008 21:03:25 von xhoster

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

Re: sending output to STDOUT or a file

am 22.04.2008 21:04:15 von 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

Re: sending output to STDOUT or a file

am 23.04.2008 06:45:01 von Charles DeRykus

On Apr 22, 11:26 am, 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;
> 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

Re: sending output to STDOUT or a file

am 23.04.2008 20:34:52 von Vahid Moghaddasi

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

Re: sending output to STDOUT or a file

am 23.04.2008 23:39:58 von xhoster

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