先看例子
#include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> int main() { pid_t status; status = system("./test.sh"); if (-1 == status) { printf("system error!"); } else { printf("exit status value = [0x%x]\n", status); //status 低7bits表示导致任务退出的信号数值,右起第8bit表示是否生成coredump文件; //高8位是实际的exit参数值。宏的时间参见标注c头文件 if (WIFEXITED(status)) //正确退出 { if (0 == WEXITSTATUS(status)) //操作成功 { printf("run shell script successfully.\n"); } else //操作失败 { printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status)); } } else //错误退出,这里属于信号中断了,WEXITSTATUS一定是0了 { printf("exit status = [%d]\n", WEXITSTATUS(status)); } } return 0; }