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