system函数返回值

背景:工作中遇到一个system函数执行udhcp失败返回256的问题,一开始以为这个函数有问题,

经过一番分析,查到system返回256,相当于shell命令返回值为1

std::system - cppreference.comhttps://en.cppreference.com/w/cpp/utility/program/system

返回值可以使用WEXITSTATUS 解析的

Return value

Implementation-defined value. If command is a null pointer, returns a nonzero value if and only if the command processor exists.

Notes

On POSIX systems, the return value can be decomposed using WEXITSTATUS and WSTOPSIG

The related POSIX function popen makes the output generated by command available to the caller.

An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O.

waiticon-default.png?t=LA92https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html 

WEXITSTATUS(stat_val)

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

这儿意思是拿低8位,猜测应该有可能有大小端?正好256,拿高八位 为1,暂时这么理解吧

system返回256_mct123的专栏-CSDN博客http://blog.chinaunix.net/uid-24774106-id-3048281.html?page=3遇到system调用脚本,结果返回了256的情况,那么如何判断脚本是否正常成功调用,即取到脚本的真正返回值system是个综合的操作,分解开来看就是相当于执行了1 fork  生成一个子进程。2 在子进程执行 execl("/bin/sh","sh","-chttps://blog.csdn.net/mct_blog/article/details/54408255

 

  1. ls: 无法访问/noexist: 没有那个文件或目录
  2. libin@libin:~/program/C/Linux/system$ echo $?
  3. 2
  4. libin@libin:~/program/C/Linux/system$

我们看到了,虽然/noexist文件并不存在,ls这条命令执行出了错,但是仍然属于shell顺利执行完毕。 ls /noexist的错误吗是2,所以,system函数的返回值为 2*256 = 512.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值