----- Original Message ----- From: isao <[EMAIL PROTECTED]> Date: Tuesday, July 1, 2003 7:07 pm Subject: stdout+stderr to file?
> I'm writing scripts that are basically wrappers for Linux shell > commands. > I want to be able to 1) print messages to screen along with say > the first > line of any STDERR, and 2) print messages, STDOUT, and STDERR to > go to a > file. The easeast thing would be to use IPC::Open3, you cna chekc out perldoc perlipc for other methods of redirection. > > The best I've come up with is something like the followng, which > doesn'tinclude STDOUT... > > > > open (LOG, ">$locations{log_file}") or die "[fail]..."; > #open (STDERR, ">&LOG") or die "[fail] Can't dup STDERR"; > #open (STDOUT, ">&LOG") or die "[fail] Can't dup STDOUT"; > select (LOG); $| = 1; # make unbuffered > > sys ("cp foobar baz", > 'copy to baz', 0); > > sub sys > { > my ($cmd, $english, $fatal) = (@_, 1); > feedback ($english); > print LOG `$cmd 2>&1`; > if ($? == '0') { thats not how you check for the exit status. since you are on a linux system you can probebly use POSIX::WEXITSTATUS > feedback ("[okay] $cmd"); > return 1; > } else { > feedback ("[fail] $cmd"); > feedback ("[error] $!$^E"); > $failed = 1; > cleanup() if ($fatal==1); > return 0; > } > } > > sub feedback > { > my $line = sprintf ("%s %s", (strftime "%H:%M", localtime), > $_[0]); print LOG "$line\n"; you should probebly use printf here > $line = (substr $line, 0, 79)."\n"; > $emailtext .= $line; > print STDOUT $line; > } > HTH, Mark G > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]