sending output to STDOUT or a file

V

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,
 
X

xhoster

Vahid said:
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.
 
J

John W. Krahn

Vahid said:
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
 
C

comp.llang.perl.moderated

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

Vahid Moghaddasi

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

xhoster

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top