简体   繁体   English

在C程序中获取系统命令输出

[英]Get system command output in C program

Is there a better way to do it? 有没有更好的方法呢?

int numOfCPU;
system("grep -c ^processor /proc/cpuinfo >> /tmp/cpuinfo");
FILE *fp = fopen("/tmp/cpuinfo", "r");
fscanf(fp, "%d", &numOfCPU);
fclose(fp);
system("rm /tmp/cpuinfo");

I don't want to create an intermediary file and then remove it. 我不想创建中间文件然后将其删除。

EDIT: 编辑:

Its not about reading from the file. 它不是从文件中读取。 The command can be "ls" or "echo 'Hello world'" 命令可以是“ls”或“echo'Hello world'”

Ok, I was confused in my other answer. 好吧,我在其他答案中感到困惑。 In any case, the philosophy in this answer is the same. 无论如何,这个答案的哲学是一样的。 You can use directly the popen function. 您可以直接使用popen功能。

Then you have something like this: 然后你有这样的事情:

int numOfCPU;
FILE *fp = popen("grep -c ^processor /proc/cpuinfo", "r");

fscanf(fp, "%d", &numOfCPU);
pclose(fp);

I hope it will be useful. 我希望它会有用。

You need to use redirection and pipes to do what you are trying to do. 您需要使用重定向和管道来执行您要执行的操作。

The popen call can help you, but if you want something more flexible, such as also redirecting input, or more secure, such as not running a string in the shell, you should follow this example, taking from the manual page of pipe. popen调用可以帮助你,但是如果你想要更灵活的东西,比如重定向输入,或者更安全,比如不在shell中运行字符串,你应该遵循这个例子,从管道的手册页中获取。

   #include <sys/wait.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <string.h>

   int
   main(int argc, char *argv[])
   {
       int pipefd[2];
       pid_t cpid;
       char buf;

       if (argc != 2) {
        fprintf(stderr, "Usage: %s <string>\n", argv[0]);
        exit(EXIT_FAILURE);
       }
       if (pipe(pipefd) == -1) {
           perror("pipe");
           exit(EXIT_FAILURE);
       }

       cpid = fork();
       if (cpid == -1) {
           perror("fork");
           exit(EXIT_FAILURE);
       }

       if (cpid == 0) {    /* Child reads from pipe */
           close(pipefd[1]);          /* Close unused write end */

           while (read(pipefd[0], &buf, 1) > 0)
               write(STDOUT_FILENO, &buf, 1);

           write(STDOUT_FILENO, "\n", 1);
           close(pipefd[0]);
           _exit(EXIT_SUCCESS);

       } else {            /* Parent writes argv[1] to pipe */
           close(pipefd[0]);          /* Close unused read end */
           write(pipefd[1], argv[1], strlen(argv[1]));
           close(pipefd[1]);          /* Reader will see EOF */
           wait(NULL);                /* Wait for child */
           exit(EXIT_SUCCESS);
       }
    }

You should modify the child process to use dup2 to redirect the standard output to the pipe and then exec the command you want it to run. 您应该修改子进程以使用dup2将标准输出重定向到管道,然后执行您希望它运行的命令。

Using awk : 使用awk

#include <stdlib.h>
#include <stdio.h>

void main()
{
    int numOfCPU =0;

    system ("awk '/processor/{numOfCPU++}END{print numOfCPU}' /proc/cpuinfo");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM