linux system函数是否执行成功判断方法

首先我们看一个例子:

    status = system("./test.sh");  


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).
看得很晕吧?

system函数对返回值的处理,涉及3个阶段:
阶段1:创建子进程等准备工作。如果失败,返回-1。
阶段2:调用/bin/sh拉起shell脚本,如果拉起失败或者shell未正常执行结束(参见备注1),
             原因值被写入到status的低8~15比特位中。system的man中只说明了会写了127这个值,
             但实测发现还会写126等值。
阶段3:如果shell脚本正常执行结束,将shell返回值填到status的低8~15比特位中。
备注1:
只要能够调用到/bin/sh,并且执行shell过程中没有被其他信号异常中断,都算正常结束。
比如:不管shell脚本中返回什么原因值,是0还是非0,都算正常执行结束。
即使shell脚本不存在或没有执行权限,也都算正常执行结束。
如果shell脚本执行过程中被强制kill掉等情况则算异常结束。

如何判断阶段2中,shell脚本是否正常执行结束呢?系统提供了宏:
WIFEXITED(status)。如果WIFEXITED(status)为真,则说明正常结束。

如何取得阶段3中的shell返回值?你可以直接通过右移8bit来实现,

但安全的做法是使用系统提供的宏:WEXITSTATUS(status)。

需要包含头文件:#include <sys/wait.h>



由于我们一般在shell脚本中会通过返回值判断本脚本是否正常执行,如果成功返回0,失败返回正数。
所以综上,判断一个system函数调用shell脚本是否正常结束的方法应该是如下3个条件同时成立:
(1)-1 != status
(2)WIFEXITED(status)为真
(3)0 == WEXITSTATUS(status)

注意:
根据以上分析,当shell脚本不存在、没有执行权限等场景下时,以上前2个条件仍会成立,
此时WEXITSTATUS(status)为127,126等数值。
所以,我们在shell脚本中不能将127,126等数值定义为返回值,否则无法区分中是shell的返回值,

还是调用shell脚本异常的原因值。shell脚本中的返回值最好多1开始递增。


***********************************************************************************************************************

以上为个人转载,得出以下函数:

int System_Check(int result)
{
    if((-1 != result) && (WIFEXITED(result)) && (!(WEXITSTATUS(result))))
        return 0;
    else
        return -1;
}

使用形式:

int result =0;

result = system(cmd);

if(!System_Check(result))

    ****成功****

else

    ****失败****


这个判断标准在绝大多数情况下,成立!也有不满足的情况,例如:

使用system函数执行:diff file1 file2 命令,如果两个文件有差异,该命令执行成功。

而返回result不符合上述检测。


文章如有不当之处,欢迎指正

原文地址https://blog.csdn.net/yuanbinquan/article/details/40919311

********************************************************

WIFEXITED

********************************************************

WIFEXITED/WEXITSTATUS/WIFSIGNALED  
2008-06-13 15:07  
If the exit status value (*note Program Termination::) of the child  
process is zero, then the status value reported by `waitpid' or `wait'  
is also zero. You can test for other kinds of information encoded in  
the returned status value using the following macros. These macros are  
defined in the header file `sys/wait.h'.  
-- Macro: int WIFEXITED (int STATUS)  
     This macro returns a nonzero value if the child process terminated  
     normally with `exit' or `_exit'.  
-- Macro: int WEXITSTATUS (int STATUS)  
     If `WIFEXITED' is true of STATUS, this macro returns the low-order

8 bits of the exit status value from the child process. *Note  
     Exit Status::.  
-- Macro: int WIFSIGNALED (int STATUS)  
     This macro returns a nonzero value if the child process terminated  
     because it received a signal that was not handled. *Note Signal  
     Handling::.  
子进程的结束状态返回后存于status,底下有几个宏可判别结束情况  
WIFEXITED(status)如果子进程正常结束则为非0值。  
WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。  
WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真 

WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。  
WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。  
WSTOPSIG(status)取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED 来判断后才使用此宏。

原地址:http://blog.sina.com.cn/s/blog_4da4ea3c0101ojnr.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值