system函数
system两层含义:
1、正确退出后。还需要再判断,操作成功或者操作失败。
2、错误退出。
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h> int main()
{
pid_t status; status = system("./test.sh"); if (-1 == status)
{
printf("system error!");
}
else
{
printf("exit status value = [0x%x]\n", status); if (WIFEXITED(status)) //正确退出
{
if (0 == WEXITSTATUS(status)) //操作成功
{
printf("run shell script successfully.\n");
}
else //操作失败
{
printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));
}
}
else //错误退出
{
printf("exit status = [%d]\n", WEXITSTATUS(status));
}
} return 0;
}
下面更详细解释:
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过程中没有被其他信号异常中断,都算正常结束。
比如:
参考链接:http://www.embeddedlinux.org.cn/emb-linux/system-development/201310/10-2635.html
system函数的更多相关文章
- 关于linux下system()函数的总结
导读 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.这里必须要搞懂system()函数,因为有时你不得不面对它. 先来看一下system()函数的简单 ...
- C语言中的system函数参数及其作用
函数名: system 功 能: 发出一个DOS命令 用 法: int system(char *command); system函数已经被收录在标准c库中,可以直接调用 system() ...
- linux下使用fork,exec,waitpid模拟system函数
代码如下: #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include &l ...
- Linux system函数详解
system 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型 int system(cons ...
- Linux system 函数的一些注意事项
在日常的代码编程中 , 我们可以利用system 函数去调用一些我们自己想调用的命令 , 并获取他的返回值. 函数的原型如下: int system(const char *command); 上一 ...
- 关于system函数的安全性漏洞
当以一个普通用户去执行 设置-用户ID 为root的程序时,如果再次用了system函数时,被system函数所执行的那个程序具有 有效-用户ID 为root的风险(虽然真实用户还是普通用户),这也 ...
- C语言中system()函数的用法总结(转)
system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码: #include <sys/types.h> #include <sys/wait ...
- system 函数
相关函数:fork, execve, waitpid, popen 头文件:#include <stdlib.h> 定义函数:int system(const char * string) ...
- SUID或SGID程序中能不能用system函数
system()函数的声明和说明如下: 注意它的描述那里,system()执行一个由command参数定义的命令,通过调用/bin/sh -c命令来实现这个功能.也就是说它的逻辑是这样的! 进程调用s ...
随机推荐
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
- CSS3教程:pointer-events属性值详解 阻止穿透点击
转:http://www.poluoluo.com/jzxy/201109/142876.html 在某个项目中,很多元素需要定位在一个地图层上面,这里就要用到很多绝对定位或者相对定位的元素,但是这样 ...
- tomcat的配置详解:[1]tomcat绑定域名
转自:http://jingyan.baidu.com/article/7e440953dc096e2fc0e2ef1a.html tomcat的配置详解:[1]tomcat绑定域名分步阅读 在jav ...
- Python3字典中items()和python2.x中iteritems()有什么区别
在Python2.x中,items( )用于 返回一个字典的拷贝列表[Returns a copy of the list of all items (key/value pairs) in D],占 ...
- java.util.concurrent Class ThreadPoolExecutor
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
- *.bz2和*.gz分别是什么压缩格式
这两个都是linux常用的压缩格式,通常用来压缩源代码包,因为源代码文件过多,它们还经常跟tar命令结合使用所以一般下载linux的源代码就有.tar.bz2,.tar.gz这样的格式其中bz2格式的 ...
- unity, 由unity5.2.1升级到5.4.2物体变亮解法
由unity5.2.1升级到5.4.2之后,使用standard shader的物体会变亮. 原因如图: 框中两项是5.4.2多出来的,如果把specular Highlights的勾选去掉,就跟以前 ...
- 【转】图解CSS的padding,margin,border属性(详细介绍及举例说明)
W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层.盒模型主要定义四个区域:内容(content).边框距(p ...
- OpenJudge计算概论-完美立方【暂时就想到了枚举法了】
/*===================================== 完美立方 总时间限制: 1000ms 内存限制: 65536kB 描述 a的立方 = b的立方 + c的立方 + d的立 ...
- J2EE分布式事务中的提交、回滚方法调用异常。
这个是昨天上班的时候,写一个后台程序的调试程序时碰到的问题,和项目经理纠结了一天,最后搞定了.于是今天上班正好闲着,花了几乎一天的时间去网上找各种相关的资料.目前了解的内容如此: 根据使用的weblo ...