zoukankan      html  css  js  c++  java
  • system执行shell命令

    system - execute a shell command

    #include <stdlib.h>

    int system (const char *command);

    描述

    The system() uses fork to create a child process that executes a command specified in command using execl as follows:

    execl("/bin/sh", "sh", "-c", command, (char *) 0);

    and returns after the command has been completed.

    During excution of the comman, SIGCHLD will be bocked, and SIGINT and SIGQUIT will be ignored.

    If command is NULL, the system() returns a status indicating whether a shell is available on the system.

    注意:system在执行完command后才会返回。易造成system进程阻塞,可在command后增加&,以让system立即返回。

    返回值

    》假如command是NULL,非零值指示shell(执行cmd的/bin/sh)可用,0指示shell不可用。一般情况都返回非0(NULL时),因为系统shell可用。

    》假如chilid进程创建失败(fork失败),或child进程状态不能获取(不知fork进程是否创建成功,如内存不足等或未知原因),返回-1。一般不会出现此情况,但应判断此异常情况,判断返回值不为-1。

    》假如shell不能在child子进程中执行,此时类似子进程调用_exit(127)退出。比如执行的shell命令不存在。system返回0x7F00(32512),shell退出代码为127.

    》system调用成功,返回值是执行command的shell的结束状态(shell的退出状态是shell执行的最后command的终止状态)。

    最后两情况,返回值是“wait status"。wait status可以用waitpid宏检测(如WIFEXITED(),WEXITSTATUS()等)。

    system() does not affect the wait status of any other children.

    注意

    As mentioned, system() ignores SIGINT and SIGQUIT. This may make programs that call it from a loop uninterruptible, unless they take care themselves to check the exit status of the child. E.g.

      while(something) {

        int ret = system("foo");

        if(WIFSIGNALED(ret) && (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))

          break;

      }

    It is possible for the shell command to return 127, so that code is not a sure indication that the execve call failed.

    一般应用举例(system执行时,fork必须成功(不能返回-1);一般shell成功返回0,判断不为0退出):

    ret = system(cmd);
    
    if(ret < 0){
    
      exit (1); 
    
    }
    
    
    if(WEXITSTATUS(ret) != 0){
    
      printf("cmd failed.
    ");
    
      exit(1);
    
    }

    system立刻返回的举例:

    #include <stdio.h>
    #include <stdlib.h>
    #include <wait.h>
    
    #define CMD "./hao.sh&"
    
    int main(int argc, char *argv[])
    {
    
        int ret = 0;
        if(argc == 1){ 
            ret = system(CMD);
    //      ret = system(NULL);
        } else {
            ret = system(argv[1]);
        }   
    
        printf("return:%d
    ", ret);
        if(ret == 0){ 
            printf("Success
    ");
        } else {
            printf("Fail
    ");
            if(WIFEXITED(ret)){
                printf("error status:%d
    ", WEXITSTATUS(ret));
            }   
            if(WIFSIGNALED(ret)){
                printf("error signal:%d
    ", WTERMSIG(ret));
            }   
        }   
        return 0;
    }
  • 相关阅读:
    转载~基于比较的排序算法的最优下界为什么是O(nlogn)
    关于 cgdb & gdbtui 的输入scanf()问题
    制定ip池内随机生成ip地址
    C 随机不重复元素~转
    随机选取算法 (有权重的记录中选取)~转
    全局变量的教训
    Python字符串的encode与decode研究心得——解决乱码问题
    Python 求最大公因式~辗转相除法
    Python格式化字符串~转
    Python 中的枚举类型~转
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/6155473.html
Copyright © 2011-2022 走看看