I'm a beginner in Perl. I have a Windows batch script which contains multiple NMake commands. An existing issue with this batch script is that even if the NMake command fails during its execution, ERRORLEVEL
doesn't get set properly.
So we never know whether the command worked until we parse the log file. I looked into it but couldn't find a solution. I, then thought of converting this batch script to a Perl script assuming that trapping error will be easier but it seems it's not that easy :)
Whenever I run my Perl script, the 'system' command always returns 0. I looked at many different links, and realized that capturing the correct return status of 'system' command is not that straightforward. Still, I tried the suggestions but things are not working. :(
Let me mention that the NMake command that is called, in turn, calls many different commands during its execution. For instance, the command output mentioned below, which is throwing 'fatal error', is actually part of a Perl script (check_dir.pl). This call to Perl script is written in the NMake file itself.
If I call this Perl file (check_dir.pl
) directly and check for exit value, I get correct result i.e., the command fails and prints a non-zero exit value (...unexpectedly returned exit value 2).
Tried Perl's system function but it didn't help. I used the following code:
system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");
if ( $? == -1 ) {
print "Command failed to execute: $!\n";
}
elsif ( $? & 127 ) {
printf "The child died with signal %d, %s a coredump\n",
( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}
Output:
..... ..... Unable to open dir: R:\TSM_Latest Compressing...NMAKE : fatal error U1077: 'if' : return code '0x2' Stop. child exited with value 0
Also tried:
use IPC::System::Simple qw(system);
my $exit_status = system ("nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1");
if ($exit_status != 0) {
print "Failure";
exit 3;
} else {
print "Success";
}
Finally tried the following module:
use IPC::Run qw( run timeout );
run "nmake /f _nt.mak pack_cd SUB_PLAT=$PLAT DR=$plat 2>&1" or die "NMake returned $?";
Nothing seems to be working :(
Please correct me if i'm interpreting the return value of system
incorrectly.
You have:
Given that you don't seem to care about the actual output, you can try
To make sure you bypass
cmd.exe
and see if you get something useful.For reference, the exit codes from nmake are listed here.
Running the following program:
produces:
The following version:
produces:
In fact, even when I use
I get the same correct and expected output.
Ditto when I use
These observations lead me to the following questions:
Which version of nmake are you using?
Is the
/I
option in effect? Even though you don't set it from the command line, note the following:So, I put together the following files:
And, ran:
which gave me the output:
where the last line shows that the exit status of nmake was correctly propagated.
Conclusion:
You have some other problem.
In fact, the OP later pointed out in comments that:
Given
tee
s involvement in the pipeline, it is not surprising thatnmake
s exit code gets lost.tee
is successfully able to process output fromnmake
, so it returns success, and that's the exit code your script sees.Therefore, the solution is to capture the output of
nmake
yourself, either usingqx
(coupled with the appropriate level of error checking), or usingcapture
fromIPC::System::Simple
. Then, you can decide to whether you want to print that output, save to a file, put it in an email etc …