system/WIFEXITED/WEXITSTATUS函数-linux
来源:互联网 发布:纤莉秀大码女装淘宝网 编辑:程序博客网 时间:2024/06/18 06:54
1、正确退出后。还需要再判断,操作成功或者操作失败。2、错误退出。
下面更详细解释:
1、先统一两个说法:
(1)system 返回值:指调用system函数后的返回值,比如上例中status为system返回值
(2)shell 返回值:指system所调用的shell命令的返回值,比如上例中,test.sh中返回的值为shell返回值。
2、如何正确判断test.sh是否正确执行?
都错!(仅仅判断status是否==0?或者仅判断status是否!=-1? )
3、man中对于system的说明
RETURN VALUE
The value returned is -1 on error (e.g. fork() failed), and the return
status of the command otherwise. This latter return status is in the
format specified in wait(2). Thus, the exit code of the command will
be WEXITSTATUS(status). In case /bin/sh could not be executed, the
exit status will be that of a command that does exit(127).
看得很晕吧?
4、system函数对返回值的处理。
阶段1:
创建子进程等准备工作。如果失败,返回-1。
阶段2:
调用/bin/sh拉起shell脚本,如果拉起失败或者shell未正常执行结束(参见备注1),原因值被写入到status的低8~15比特位中。
如何判断阶段2中,shell脚本是否正常执行结束呢?系统提供了宏:WIFEXITED(status)。如果WIFEXITED(status)为真,则说明正常结束。
阶段3:
如果shell脚本正常执行结束,将shell返回值填到status的低8~15比特位中。
如何取得阶段3中的shell返回值?你可以直接通过右移8bit来实现,但安全的做法是使用系统提供的宏:WEXITSTATUS(status)。
备注1:
只要能够调用到/bin/sh,并且执行shell过程中没有被其他信号异常中断,都算正常结束。
比如:
不管shell脚本中返回什么原因值,是0还是非0,都算正常执行结束。即使shell脚本不存在或没有执行权限,也都算正常执行结束。
如果shell脚本执行过程中被强制kill掉等情况则算异常结束。
WIFEXITED(stat_val) Evaluates to a non-zero value if statuswas returned for a child process thatterminated normally.WEXITSTATUS(stat_val) If the value of WIFEXITED(stat_val) isnon-zero, this macro evaluates to thelow-order 8 bits of the status argumentthat the child process passed to _exit()or exit(), or the value the childprocess returned from main().
- system/WIFEXITED/WEXITSTATUS函数-linux
- system/WIFEXITED/WEXITSTATUS函数-linux
- system/WIFEXITED/WEXITSTATUS函数-linux
- system/WIFEXITED/WEXITSTATUS函数-linux
- system/WIFEXITED/WEXITSTATUS函数-linux
- system/WIFEXITED/WEXITSTATUS函数-linux
- system函数,WIFEXITED和WEXITSTATUS
- system()与WIFEXITED/WEXITSTATUS/WIFSIGNALED
- Linux下wait, WIFEXITED, WEXITSTATUS的比较
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- WIFEXITED WEXITSTATUS WIFSIGNALED
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- wait, WIFEXITED, WEXITSTATUS
- WIFEXITED/WEXITSTATUS/WIFSIGNALED
- ScreenRecorder (win7 录屏工具)
- Redis的服务安装
- thinkphp连接多数据库
- C++整形转字符串
- MarkDown编辑器的一些使用记录
- system/WIFEXITED/WEXITSTATUS函数-linux
- Android studio环境变量配置详解
- 数据库笔记(SQL之触发器)
- js 对象判空
- vs2015/mfc加减乘除简单计算器
- 华为手机logcat不打印错误日志方法
- 使用OpenSSL生成证书,并且配置到tomcat,下载ipa
- CEF方面的研究(一) 什么是CEF?
- SparkStreaming之TCP流式处理(netcat)