1834885 Members
2865 Online
110071 Solutions
New Discussion

Re: PERL - question

 
Anand_30
Regular Advisor

PERL - question

Hi,

I used the following line in my PERL script to write the errors generated from a system command to a file "log"

system("/bin/cvs checkout -r$bra ivrsrc >>$log") || die "Cannot checkout CVS code from -r$bra";

But the messages from the system command is not written to the log file. It is instead appearing in the screen.

Can anyone please help.

Thanks,
Anand
8 REPLIES 8
Rodney Hills
Honored Contributor

Re: PERL - question

You need to redirect STDERR too.

system("/bin/cvs checkout -r$bra ivrsrc >>$log 2>&1")

HTH

-- Rod Hills
There be dragons...
Anand_30
Regular Advisor

Re: PERL - question

Hi Rod,

I tried it but does not seem to work. It does not print the messages on the STDERR now but does not write the messages to the log file either.

Thanks,
Anand
Rodney Hills
Honored Contributor

Re: PERL - question

Oops,

I forgot the "&" needs to be escaped...

system("/bin/cvs checkout -r$bra ivrsrc >>$log 2>\&1")

(Otherwise perl interprets it as a subroutine call)

-- Rod Hills
There be dragons...
Gopi Sekar
Honored Contributor

Re: PERL - question


your problem is: CVS outputs the messages on standard error and >> will capture only standard output. To capture standard error you should use 2>.

Try this way:

system("/bin/cvs checkout -r$bra ivrsrc >>$log 2>>$log") || die "Cannot checkout CVS code from -r$bra";

Hope this helps,
Gopi
Never Never Never Giveup
Anand_30
Regular Advisor

Re: PERL - question

Hi Rod & Gopi,

Thanks for your help. But nothing seems to work.

I am trying it still now. The standard output seems to go to the log file but the standard error is getting displayed in the screen.

Thanks,
Anand
harry d brown jr
Honored Contributor

Re: PERL - question

You need to do something other than

system(...) || die ...;

Try something like this:

#!/usr/bin/perl
#
# this does not die:
#
system("echo hi mom >>/tmp/log 2>&1");
$errcode = $?;
printf("err code is ${errcode}\n");
eval ${errcode} == 0 || die "Cannot checkout CVS code from xyz";
#
# this DOES die:
#
system("NOREALCOMMAND hi mom >>/tmp/log 2>&1");
$errcode = $?;
printf("err code is ${errcode}\n");
eval ${errcode} == 0 || die "Cannot checkout CVS code from xyz";


live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: PERL - question

Or better yet:

eval `/bin/cvs checkout -r${bra} ivrsrc >>${log} 2>&1` || die "Cannot checkout CVS code from -r${bra}";

live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: PERL - question

I take back my last post. Use the system() ones I posted.

live free or die
harry d brown jr
Live Free or Die