|
Prev: Question about wwwboard perfuctory
Next: FAQ 2.1 What machines support perl? Where do I get it?
From: Vahid on 22 Apr 2008 14:26 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,
From: xhoster on 22 Apr 2008 15:03 Vahid <vahid.moghaddasi(a)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.
From: John W. Krahn on 22 Apr 2008 15:04 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
From: comp.llang.perl.moderated on 23 Apr 2008 00:45 On Apr 22, 11:26 am, Vahid <vahid.moghadd...(a)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
From: Vahid Moghaddasi on 23 Apr 2008 14:34 On Apr 22, 3:03 pm, xhos...(a)gmail.com wrote: > Vahid <vahid.moghadd...(a)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?
|
Next
|
Last
Pages: 1 2 Prev: Question about wwwboard perfuctory Next: FAQ 2.1 What machines support perl? Where do I get it? |