Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Get Return Value from Unix

by Noame (Beadle)
on May 01, 2008 at 09:41 UTC ( #683892=perlquestion: print w/ replies, xml ) Need Help??
Noame has asked for the wisdom of the Perl Monks concerning the following question:


I'm writing Perl program to execute 'nmake' command. I'm looking for the best way to execute the 'nmake' command from Perl program on Unix and to get the return value From the Unix shell. Finally, I want to print the error to log file and to the screen.

Currently I have written the following code and its work fine:
open (LOG, "> $buildLog") || die "-E- Can't open (write) the log file: + $buildLog\n($!)\n"; tee(STDOUT, '>>', $buildLog); tee(STDERR, '>>', $buildLog); $status = system ("nmake /f $object.mak CFG=\"$object - ${BUILD_CFG}$b +uild_config\""); if ($status) { TraceLog ("---------------------------------------"); TraceLog ("-E- nmake ended with errors! (status=$status)"); TraceLog ("---------------------------------------"); print ; &sendBuildFailure ($object); close (LOG); exit 1;
The question is if it's correct to "send" all STDOUT & STDERR to log file in that way? And after I pass successfully that function how can I continue the script without print all to log file (Stop the 'tee' function) due to the command below:
tee(STDOUT, '>>', $buildLog); tee(STDERR, '>>', $buildLog);
Please advice. Let me know if you need more information's. Thanks

Comment on Get Return Value from Unix
Select or Download Code
Re: Get Return Value from Unix
by mscharrer (Hermit) on May 01, 2008 at 11:27 UTC
    Please use the <code> tag only for your code and code like things -- not for your whole post! It's quite difficult to read and confusing.

    You also should include the used packages so we know exactly what you use. I personally have no idea from where do you get the tee and TraceLog functions and a quick search on CPAN, perldoc and google didn't got any specific results.

    I personally would reroute STDOUT and STDERR directly to the log file using the shell command, i.e. inside the system call with &>logfile. In general this task looks like a shell script would be better suited for it than a Perl program.

      Thanks for your quick respond. I'm using the following package: use File::Tee qw(tee); "TraceLog" is an internal function that add time to the log. Your recommendation to use "system" command isn't good to me because it's print the error to log file and not return -1 if the build is failed… Or I'm wrong?
        perldoc -f system says:
        The return value is the exit status of the program as returned by the "wait" call. To get the actual exit value, shift right by eight.


        Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).

        So if nmake returns a failure it should return a positive integer, not -1, which would only returned if nmake wasn't found or similar.

        I would code this like that: (untested)

        my $pid = open (NMAKE, '-|', "nmake .... 2>&1"); if (!defined $pid) { warn "Couldn't start nmake"; } while (<NMAKE>) { print STDERR $_; print STDOUT $_; } close (NMAKE); my $status = $?; if ($status) { # ... }
Re: Get Return Value from Unix
by Anonymous Monk on May 01, 2008 at 11:53 UTC
    ...execute the 'nmake' command from Perl program on Unix

    nmake? Has MS gone Unix, or whut...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://683892]
Approved by McDarren
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-09-18 07:41 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (109 votes), past polls