先看例子

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h> int main()
{
pid_t status; status = system("./test.sh"); if (- == status)
{
printf("system error!");
}
else
{
printf("exit status value = [0x%x]\n", status);
//status 低7bits表示导致任务退出的信号数值,右起第8bit表示是否生成coredump文件; //高8位是实际的exit参数值。宏的时间参见标注c头文件 if (WIFEXITED(status)) //正确退出
{
if ( == 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 ;
}

下面更详细解释:

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掉等情况则算异常结束。

linux c system返回值问题总结的更多相关文章

  1. [转]Linux命令的返回值

    Linux命令的返回值 对于某些监测脚本和探测命令蛮有用的: 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序在结束时,都会返回一个数字值,这个值叫做返回值,或者称 ...

  2. python os.system()返回值判断

    最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...

  3. system返回值校验

    int xsystem(const char *cmd){    int err; err = system(cmd); if (err == -1) {    fprintf(stderr, &qu ...

  4. linux命令执行返回值(附错误对照表)

    转自:http://blog.sina.com.cn/s/blog_6739945f0100zt4b.html 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序在结 ...

  5. Linux命令的返回值

    转摘自:http://hi.baidu.com/suchshow/item/230255b6caab369218469732 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令, ...

  6. Linux Shell函数返回值

    转:http://blog.csdn.net/ithomer/article/details/7954577 Shell函数返回值,一般有3种方式:return,argv,echo 1) return ...

  7. Linux Shell 函数返回值

    Shell函数返回值,常用的两种方式:return,echo 1) return 语句 shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回. 示例: #!/bin/sh fu ...

  8. linux recv函数返回值分析

    函数原型: ssize_t recv(int sockfd, void *buf, size_t len, int flags); 该函数第一个参数制定接收端套接字描述符; 第二个参数指明一个缓冲区, ...

  9. linux shell 函数返回值问题(超过255)

    最近再写一个shell测试的时候出现问题,函数返回值异常 用shell计算斐波那契数列数列,写了一个shell函数,然后调用的,验证的时候我只随便计算了几个数(10以内),确认结果是正确的就提交了,后 ...

随机推荐

  1. uva10635 LIS

    Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n c ...

  2. jquery-读取form表单中的所有数据列表

    代码: <script> $(function() { $('#submit').click(function() { var d = {}; var t = $('form').seri ...

  3. 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa

    3627: [JLOI2014]路径规划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 186  Solved: 70[Submit][Status] ...

  4. 【Android群英传】学习笔记(一)

    本系列博客为笔者在学习<Android群英传>的学习总结 Android相关工具镜像连接:http://www.androiddevtools.cn/ Dalvik与ART Dalvik包 ...

  5. 【web开发 | 移动APP开发】 Web 移动开发指南(2017.01.05更新)

    版本记录 - 版本1.0 创建文章(2016.12.30) - 版本1.1 更正了hybird相关知识:增加了参考文章(2017.01.05): + Web APP更正为响应式移动站点与页面,简称响应 ...

  6. ubuntu修改163软件源

    cd /etc/apt cat sources.list sudo su root sudo echo '' > sources.list nano sources.list 复制163软件源 ...

  7. PHP进程通信基础——信号

    PHP进程通信基础--信号 使用信号通信.可以使用kill -l 来查看当前系统的信号类型. 每个信号所代表的的详细含义,请查看我的这篇博客:http://www.cnblogs.com/roverl ...

  8. NOIp 1109

    停课后的第一场模拟赛,应该不会怎么说今天的模拟赛,除了第一题不知道那种筛法的复杂度是$log$的没敢写,其他都挺水的.另外,第三题的数据也弱化了,建议去这里再交一遍. 贴一下AC代码,有兴趣可以参考一 ...

  9. Python Day15

    JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...

  10. Spring MVC学习笔记——登录和异常处理

    1.在WEN-INF文件夹下面,添加一个login.jsp文件 <%@ page language="java" contentType="text/html; c ...