Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the value returned by system() the same as "$?"?

Tags:

perl

When I do system() calls in Perl, I usually inspect the return code according to the perldocs. Well, I thought so. Most of the time $rc!=0 is sufficient for me. Recently I helped two people here which had trouble with system() calls when running their .cgi scripts under apache. I instructed them to examine the $rc of

my $rc = system(...);

and linked them to the system() docs. Then I looked closer and noticed the docs aren't really talking about the $rc but instead about $? and I felt a bit embarassed and the following question arose:

Is there a difference between:

system(...);
if ($? == -1) {
    print "failed to execute: $!\n";
}
elsif ($? & 127) {
    printf "child died with signal %d, %s coredump\n",
        ($? & 127),  ($? & 128) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n", $? >> 8;
}

and

my $rc = system(...);
if ($rc == -1) {
    print "failed to execute: $!\n";
}
elsif ($rc & 127) {
    printf "child died with signal %d, %s coredump\n",
        ($rc & 127),  ($rc & 128) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n", $rc >> 8;
}

Or, for short, is $rc equal to $? for system()?

I looked through the docs of system, wait, and $? but it's not quite clear to me. Did I do wrong for the last 15 years by using $rc?

like image 948
PerlDuck Avatar asked Mar 21 '16 20:03

PerlDuck


People also ask

What is the return value of system?

The return value of system() is one of the following: * If command is NULL, then a nonzero value if a shell is available, or 0 if no shell is available. * If a child process could not be created, or its status could not be retrieved, the return value is -1 and errno is set to indicate the error.

How can I get system call return value?

The return value is the return value from the system call, unless the system call failed. In that case, syscall returns -1 and sets errno to an error code that the system call returned. Note that system calls do not return -1 when they succeed. If you specify an invalid sysno , syscall returns -1 with errno = ENOSYS .

What does the system function return C?

The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib.

What does OS system return in Python?

os. system() returns the (encoded) process exit value. 0 means success: On Unix, the return value is the exit status of the process encoded in the format specified for wait() .


1 Answers

Yes, the return value of system should equal $?.

However since $? does not only apply to system calls and $? is a global variable, it could be overwritten by other actions that are occurring. From perldoc -v '$?' these include:

$CHILD_ERROR

$?

The status returned by the last pipe close, backtick ("``") command, successful call to "wait()" or "waitpid()", or from the "system()" operator.

It is far safer to store the value immediately then compare:

my $rc = system('ls myfile.txt');
if ( $rc >> 8 != 0 ) {
   # do something because ls exited with an error
}
like image 67
Hunter McMillen Avatar answered Oct 04 '22 14:10

Hunter McMillen