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 comp.llang.perl.moderated on April 23, 2008, 12:45 am
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.

Another possibility:

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


--
Charles DeRykus

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