linux C程序中获取shell脚本输出

http://dogking.chinaunix.com/space.php?uid=23028407&do=blog&id=1742405

1. 前言

Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些夸张,但不可否认的是,借助脚本确实能够极大的简化一些编程工作。比如实现一个 ping程序来测试网络的连通性,实现ping函数需要写上200~300行代码,为什么不能直接调用系统的ping命令呢?通常在程序中通过 system函数来调用shell命令。但是,system函数仅返回命令是否执行成功,而我们可能需要获得shell命令在控制台上输出的结果。例如,执行外部命令ping后,如果执行失败,我们希望得到ping的返回信息。

2. 使用临时文件

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

 
  1. #define CMD_STR_LEN 1024
  2. int mysystem(char* cmdstring, char* tmpfile)
  3. {
  4.     char cmd_string[CMD_STR_LEN];
  5.     tmpnam(tmpfile);
  6.     sprintf(cmd_string, "%s > %s", cmdstring, tmpfile);
  7.     return system(cmd_string);
  8. }

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

3. 使用匿名管道

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

  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>

  7. using namespace std;

  8. int cmdoutput(char* cmdstring, char* buf, int len)
  9. {
  10.     int fd[2];
  11.     pid_t pid;
  12.     int n, count;
  13.     
  14.     memset(buf, 0, len);
  15.     
  16.     if (pipe(fd) < 0)
  17.         return -1;
  18.     if ((pid = fork()) < 0)
  19.         return -1;
  20.     else if (pid > 0)    /* parent process */
  21.     {
  22.         close(fd[1]); /* close write end */
  23.         count = 0;
  24.         char buffer[1024];
  25.         n=read(fd[0],buffer,len);
  26.         while (n>0)
  27.         {
  28.             strcat(buf,buffer);
  29.             n=read(fd[0],buffer,len);
  30.         }
  31.         close(fd[0]);
  32.         if (waitpid(pid, NULL, 0) > 0)
  33.             return 1;
  34.     }
  35.     else /* child process */ 
  36.     {
  37.         close(fd[0]); /* close read end */
  38.         if (fd[1] != STDOUT_FILENO)
  39.         {
  40.             if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
  41.             {
  42.                 return -1;
  43.             }
  44.             close(fd[1]);
  45.         }
  46.         if (execl("/bin/bash", "bash", "-c", cmdstring, (char*)0) == -1)
  47.             return -1;
  48.     }

  49.     return 0;
  50. }

  51. int main()
  52. {
  53.     char buff[1024];
  54.     int result=cmdoutput((char*)"ls", buff, strlen(buff));
  55.     
  56.     cout<<result<<endl;
  57.     printf("%s\n",buff);
  58.     
  59.     return 0;
  60. }
4. 使用popen

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

FILE *popen(const char *command, const char *type);

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

具体请参见:linux C编程--popen函数详解


5. 小结

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值