system函数返回值

  system(执行shell 命令)

  相关函数  fork,execve,waitpid,popen

  表头文件  #include<stdlib.h>

  定义函数  int system(const char * string);

  函数说明  system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。

  返回值  如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。

  附加说明  在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。

  范例  #include<stdlib.h>

  main()

  {

  system(“ls -al /etc/passwd /etc/shadow”);

  }

  执行  -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

  -r--------- 1 root root 572 Sep 2 15 :34 /etc/shadow


       Upon successful completion, the system subroutine returns the exit status of the shell. The exit status of the shell
       is returned in the same manner as a call to the wait or waitpid subroutine, using the structures in the sys/wait.h
       file.

       If the String parameter is a null pointer and a command processor is available, the system subroutine returns a
       nonzero value. If the fork subroutine fails or if the exit status of the shell cannot be obtained, the system
       subroutine returns a value of -1. If the exec l subroutine fails, the system subroutine returns a value of 127. In
       all cases, the errno global variable is set to indicate the error.

----------------------------------------------------------------------------------------------------------------------------------------------------

linux中system函数的返回值
刚用到system函数,要根据其返回值来做进一步操作,可是system的返回值并不等于其调用的程序的返回值,man了没看懂,后来在网上搜索了一下,终于看到了一个DX的理解,记录之。

引自:原文

要分成两部分来说: 
1,在程序中,用exit来设置进程的退出值时,虽然该函数的参数类型为int型,但再父进程中只能取到其值的低8位.所以用exit返回值时,高于255的值是没有意义的. 

2,对于system函数,返回值是由两部分组成的,低8位值表示所执行的脚本在执行过程中所接收到的信号值,其余的位表示的脚本exit退出时所设置的值, 

即脚本内exit退出是的值的低8位,在system返回值的低9-16位


这样我们就可以通过右移操作来得到exit的值了。

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

以下转自:http://zhangwenxin82.blog.163.com/blog/static/1145959562010323103241387/

 

1,在程序中,用exit来设置进程的退出值时,虽然该函数的参数类型为int型,但再父进程中只能取到其值的低8位.所以用exit返回值时,高于255的值是没有意义的. 

2,对于system函数,返回值是由两部分组成的,低8位值表示所执行的脚本在执行过程中所接收到的信号值,其余的位表示的脚本exit退出时所设置的值, 

即脚本内exit退出是的值的低8位,在system返回值的低9-16位.
 

Linux中调用 system的返回值   system函数返回值 - wilson - Wilsons blogsystem函数返回值 - wilson - Wilsons blog  CSDN Blog推出文章指数概念,文章指数是对Blog文章综合评分后推算出的,综合评分项分别是该文章的点击量,回复次数,被网摘收录数量,文章长度和文章类型;满分100,每月更新一次.

包含文件
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
先写一个被调用的函数
==================================
#include <iostream>
int main()
{
printf("Return 10./n");
return 10;
}
==================================
编译后生成一个"rt"的可执行文件
运行结果
==================================
Return 10.
==================================
再写一个调用system的程序
==================================
#include <stdio.h>;
#include <stdlib.h>;
#include <sys/wait.h>;
#include <sys/types.h>;

int main()
{

        pid_t status ;

       int errno = 0 ;
        status = system("./rt") ;

printf("wifexited(status):%d/n",WIFEXITED(status));
printf("WEXITSTATUS(status):%d/n",WEXITSTATUS(status));
        if (status == -1)
                printf("system error!") ;

        if (WIFEXITED(status)){
                printf("cp exit normal![%d]/n", errno) ;
                printf("exit staus = [%X]/n", WEXITSTATUS(status)) ;
        }else
                printf("cp exit illegal![%d]/n", errno) ;
}
~
[tiantao@probe sys_test]$ cat sys_test.cpp 
#include <stdio.h>;
#include <stdlib.h>;
#include <sys/wait.h>;
#include <sys/types.h>;

int main()
{
        
        pid_t status ;
        
       int errno = 0 ;
        status = system("./rt") ;
        
printf("wifexited(status):%d/n",WIFEXITED(status));
printf("WEXITSTATUS(status):%d/n",WEXITSTATUS(status));
        if (status == -1)
                printf("system error!") ;
        
        if (WIFEXITED(status)){
                printf("cp exit normal![%d]/n", errno) ;
                printf("exit staus = [%X]/n", WEXITSTATUS(status)) ;
        }else 
                printf("cp exit illegal![%d]/n", errno) ;
}

==================================
编译后运行结果
==================================
Return 10.
wifexited(status):1
WEXITSTATUS(status):10
cp exit normal![0]
exit staus = [A]
==================================
可以看到:
WEXITSTATUS(status)可以得到调用程序的返回值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值