Linux c程序中获取shell脚本输出(如获取system命令输出)

在工作中遇到一个小问题,就是想获取函数system()执行之后打印的字符串信息。

这个功能还是很实用的,能为我们节省很多开发时间,

特地整理了一下相关知识点分享给大家。

1. 使用临时文件

1.1 使用shell的重定向

首先想到的方法就是将命令输出重定向到一个临时文件,在我们的应用程序中读取这个临时文件,获得外部命令执行结果,代码如下所示:

  #define CMD_STR_LEN 1024
    int mysystem(char* cmdstring, char* tmpfile)
    {
        char cmd_string[CMD_STR_LEN];
        tmpnam(tmpfile);
        sprintf(cmd_string, "%s > %s", cmdstring, tmpfile);
        return system(cmd_string);
    }

1.2 freopen标准输出到文件

#define _GNU_SOURCE
#include <stdio.h>
 
#include <stdlib.h>
 
int main()
{
    if(freopen("file.txt","w",stdout)==NULL)
        fprintf(stderr,"error\n");
    system("ls -ahl");
    printf("This is in the file\n");      //这句话会在file.txt中显示。
    fclose(stdout);               
    return 0;
}

freopen还可以重定向标准输入。

//首先在同路径下创建一个in.txt文本文档写入若干数字
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    freopen("in.txt","r",stdin);     //从in.txt 中读入数据
    freopen("out.txt","w",stdout);  // 将最后数据写入out.txt中
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)     //数据是从in.txt中输入的
        printf("%d\n",a+b);             //写入out.txt中
    fclose(stdin);
    fclose(stdout);
    return 0;
}

这种使用使用了临时文件作为应用程序和外部命令之间的联系桥梁,在应用程序中需要读取文件,然后再删除该临时文件,比较繁琐,优点是实现简单,容易理解。有没有不借助临时文件的方法呢?

2. 使用匿名管道

在<<UNIX 环境高级编程>>一书中给出了一种通过匿名管道方式将程序结果输出到分页程序的例子,因此想到,我们也可以通过管道来将外部命令的结果同应用 程序连接起来。方法就是fork一个子进程,并创建一个匿名管道,在子进程中执行shell命令,并将其标准输出dup到匿名管道的输入端,父进程从管道 中读取,即可获得shell命令的输出,代码如下:

/**
   * 增强的system函数,能够返回system调用的输出
   *
   * @param[in] cmdstring 调用外部程序或脚本的命令串
   * @param[out] buf 返回外部命令的结果的缓冲区
   * @param[in] len 缓冲区buf的长度
   *
   * @return 0: 成功; -1: 失败 
   */
int mysystem(char* cmdstring, char* buf, int len)
{
      int   fd[2];
      pid_t pid;
      int   n, count; 
      memset(buf, 0, len);
      if (pipe(fd) < 0)
          return -1;
      if ((pid = fork()) < 0)
          return -1;
      else if (pid > 0)     /* parent process */
      {
          close(fd[1]);     /* close write end */
          count = 0;
          while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
              count += n;
          close(fd[0]);
          if (waitpid(pid, NULL, 0) > 0)
              return -1;
      }
      else                  /* child process */
      {
          close(fd[0]);     /* close read end */
          if (fd[1] != STDOUT_FILENO)
          {
              if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
              {
                  return -1;
              }
              close(fd[1]);
          } 
          if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
              return -1;
      } 
      return 0;
}

3. 使用popen

在学习unix编程的过程中,发现系统还提供了一个popen函数,可以非常简单的处理调用shell,其函数原型如下:

#include <stdio.h>
 
FILE *popen(const char *command, const char *type);
 
int pclose(FILE *stream);

该函数的作用是创建一个管道,fork一个进程,然后执行shell,而shell的输出可以采用读取文件的方式获得。采用这种方法,既避免了创建临时文件,又不受输出字符数的限制,推荐使用。

popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。

下面看一个例子:

/*******************************************************************************************
** Name:popen.c
**      This program is used to show the usage of popen() .
** Author:zieckey,(zieckey@yahoo.com.cn)
** Date:2007/9/30 11:47
** All rights reserved!
*******************************************************************************************/
#include <sys/types.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h>
 
int main( void ) 
{ 
   FILE   *stream; 
   FILE   *wstream;
   char   buf[1024]; 
     
    memset( buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
    stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
    wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件
 
    fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
    fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE    *wstream对应的流中,也是写到文件中
    
    pclose( stream );  
    fclose( wstream );
    
    return 0;
}   

使用getline函数直接从popen返回的数据流中读取数据:


```c
    #define _GNU_SOURCE
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
     
    int
    main (void)
    {
      FILE *stream;
     
      char *line = NULL;
      size_t len = 0;
      ssize_t read;


​     
      stream = popen ("ls -l", "r");
      while ((read = getline (&line, &len, stream)) != -1)
        {
          printf ("Retrieved line of length %zu :\n", read);
          printf ("%s", line);
        }
     
      pclose (stream);
     
      return 0;
    }

4. 小结

有 统计数据表明,代码的缺陷率是一定的,与所使用的语言无关。Linux提供了很多的实用工具和脚本,在程序中调用工具和脚本,无疑可以简化程序,从而降低 代码的缺陷数目。linux shell脚本也是一个强大的工具,我们可以根据需要编制脚本,然后在程序中调用自定义脚本。
例如:indent getline.c 可以整理代码缩进等风格问题 。

sed -i 's/\xc2\xa0/\x20/g' test.c 将test.c中的中文替换为空格

sed -i 's/\xc2\xa0//g' getline.c  将test.c中的中文删除
posted @   一口Linux  阅读(449)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2020-08-17 手把手教Linux驱动2-之模块参数和符号导出
2020-08-17 手把手教Linux驱动1-模块化编程,玩转module
点击右上角即可分享
微信分享提示