linux系统调用system函数的使用总结

本文详细解析了Linux系统调用`system`函数执行shell脚本的返回值处理,包括三个阶段的解释及如何判断脚本是否正确执行。通过`WIFEXITED`和`WEXITSTATUS`宏判断shell脚本执行状态,强调在脚本中避免使用127、126等特殊返回值。
摘要由CSDN通过智能技术生成

有些时候我们不得不使用系统调用system来执行shell脚本,而且还需要知道是否执行成功,下面就介绍怎么判断时候成功执行。

例:

1.  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 commandotherwise.  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 thatdoes exit(127).

看得很晕吧?

 

system函数对返回值的处理,涉及3个阶段:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值