linux命令执行成功后悔返回,system执行linux命令通过返回值判断是否成功

static intRunCommand(string& result, const string& command,bool erroutput = false)

{

if(erroutput)

command += "2>&1 ";

FILE* fp = popen((command + redirector).c_str(), "r");

if (fp == NULL)

{

result = string("run commandfailed:") + strerror(errno) + ",command:" + command;

return -1;

}

stringstream ss;

char linebuf[512];

while (fgets(linebuf, sizeof(linebuf), fp) != NULL)

{

if (ss.tellp() < (4<< 10))

{

ss << linebuf;

}

}

int ret = pclose(fp);

if (ret == -1 ||!WIFEXITED(ret))

{

result = string("run commandfailed:") + strerror(errno) + ",command:" + command;

return -1;

}

int exitcode =WEXITSTATUS(ret);

result = ss.str();

return exitcode;

}

WIFEXITED(status)如果子进程正常结束则为非0值。

WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。

WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真。

WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。

WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。

WSTOPSIG(status)取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED 来判断后才使用此宏。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值