system()库函数使用fork(2)创建一个子进程,该子进程使用execl(3)执行指定的shell命令,
execl("/bin/sh", “sh”, “-c”, command, (char *) 0);
system - execute a shell command
#include <stdlib.h>
int system(const char *command);
有了这两个宏代码就简介很多, 总结一下,system的返回值需要通过以下三个步骤确定
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
pid_t status;
status = system("./test.sh");
printf("exit status value = [0x%x]\n", status);
if (WIFEXITED(status))
{
if (0 == WEXITSTATUS(status))
{
printf("run sucess\n");
}
else
{
printf("run fail, exit code: %d\n", WEXITSTATUS(status));
}
}
else
{
printf("exit status = [%d]\n", WEXITSTATUS(status));
}
}