您的位置:首页 > 其它

根据system函数返回值确定命令是否执行成功

2018-01-31 10:45 281 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>

int system_cmd(char *cmd)
{
pid_t status;

status = system(cmd);

if (-1 == status)
{
printf("create child process fail!\n");
return -1;
}
else
{
//printf("exit status value = [0x%x]\n", status);

if (WIFEXITED(status))
{
if (0 != WEXITSTATUS(status))
{
printf("run shell script fail.\n");
return -1;
}

}
else
{
printf("exit status = [%d]\n", WEXITSTATUS(status));
return -1;
}
}

}

int main()
{
system_cmd("twe");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: