如何获取system()系统调用的子命令返回值

系统库函数system()通常用来调用一个外部命令;这是一个同步调用,函数会一直等待外部命令执行结束才返回,调用者然后检查结果是否正确;比如:
if (system(...) == 0) or
if (system(...) != -1)
是两种常见的检查运行结果是否正确的做法。

但是要想得到子命令的返回值信息,需要一些额外的处理代码。
下面是man system对返回值的描述:

RETURN VALUE
       The value returned is -1 on error (e.g.  fork(2) failed), and the return status of the command other-
       wise.  This latter return status is in the format specified in wait(2).  Thus, the exit code  of  the
       command  will be WEXITSTATUS(status).  In case /bin/sh could not be executed, the exit status will be
       that of a command that does exit(127).

       If the value of command is NULL, system() returns non-zero if the shell is  available,  and  zero  if
       not.

可以看到system()的返回值并不是命令行子进程的返回值,其中也包括系统调用出错的错误信息,system()的内部实现代码可能就会出错,比如fork()函数就出错了,在这种情况下,子进程都没有被调用起来,因此system()的返回值肯定不是命令行子进程的返回码;另外如果命令行子进程意外终止,比如收到外部信号了,甚至crash了,system()的返回值也肯定不是子进程的正常返回值。

那么如何才能准确的到命令行子进程的返回值呢?有两个宏可以用来判断:

WIFEXITED(ret)

Returns true if the child process exited normaly, the next step is to examine the actual return value using WEXITSTATUS(ret).

WIFSIGNALED(ret)

Returns true child was terminated abnormally (a signal was raised), If WIFSIGNALED(ret) is true, WTERMSIG(ret) evaluates to the number of the signal that caused the termination of the process.

完整的判断代码如下

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    int ret = system("your_external_command_line");
    if (ret == -1) {
        printf("command subprocess is not launched\n");
    }
    else if (WIFEXITED(ret)) {
        printf("command subprocess is normally terminated, exit status = %d\n", WEXITSTATUS(ret));
    }
    else if (WIFSIGNALED(ret)) {
        printf("command subprocess is abnormally terminated, signal number = %d\n", WTERMSIG(ret));
    }
    else {
        printf("command subprocess is abnormally terminated\n");
    }
    return 0;
}

推荐阅读更多精彩内容