Chinaunix首页 | 论坛 | 博客
  • 博客访问: 629466
  • 博文数量: 194
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 884
  • 用 户 组: 普通用户
  • 注册时间: 2015-09-21 16:48
文章分类

全部博文(194)

文章存档

2023年(1)

2022年(3)

2021年(16)

2020年(4)

2019年(5)

2018年(10)

2017年(72)

2016年(36)

2015年(47)

我的朋友

分类: LINUX

2016-08-24 17:55:50

转自:

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

点击(此处)折叠或打开

  1. /** * 增强的system函数,能够返回system调用的输出 *
  2. * @param[in] cmdstring 调用外部程序或脚本的命令串
  3. * @param[out] buf 返回外部命令的结果的缓冲区
  4. * @param[in] len 缓冲区buf的长度
  5. * * @return 0: 成功; -1: 失败 */
  6. int mysystem(char* cmdstring, char* buf, int len)
  7. {
  8.     int fd[2]; pid_t pid;
  9.     int n, count;
  10.     
  11.     memset(buf, 0, len);
  12.     if (pipe(fd) < 0)
  13.     return -1;
  14.     
  15.     if ((pid = fork()) < 0)
  16.         return -1;
  17.     else if (pid > 0) /* parent process */
  18.     {
  19.         close(fd[1]); /* close write end */
  20.         count = 0;
  21.         while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
  22.         count += n;
  23.         close(fd[0]);
  24.         if (waitpid(pid, NULL, 0) > 0)
  25.             return -1;
  26.     }
  27.     else /* child process */
  28.     {
  29.         close(fd[0]); /* close read end */
  30.         if (fd[1] != STDOUT_FILENO)
  31.         {
  32.             if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
  33.         {
  34.         return -1;
  35.         }
  36.         close(fd[1]);
  37.         }
  38.         if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
  39.             return -1;
  40.     }
  41.     return 0;
  42. }
4. 使用popen
在学习unix编程的过程中,发现系统还提供了一个popen函数,可以非常简单的处理调用shell,其函数原型如下:
FILE *popen(const char *command, const char *type);
该函数的作用是创建一个管道,fork一个进程,然后执行shell,而shell的输出可以采用读取文件的方式获得。采用这种方法,既避免了创建临时文件,又不受输出字符数的限制,推荐使用。
popen使用FIFO管道执行外部程序。

点击(此处)折叠或打开

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

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

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. int main( void )
  7. {
  8.     FILE *stream;
  9.     FILE *wstream;
  10.     char buf[1024];
  11.     memset( buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
  12.     stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
  13.     wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件
  14.     fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
  15.     fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE *wstream对应的流中,也是写到文件中
  16.     pclose( stream );
  17.     fclose( wstream );
  18.     return 0;
  19. }
过程如下:
wanglinbin@SDserver:~/test/C-shell$ gcc popen.c -o popen
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ ls
popen*  popen.c*
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ ./popen
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ 
wanglinbin@SDserver:~/test/C-shell$ cat test_popen.txt
总用量 16
-rwxr-xr-x 1 wanglinbin wanglinbin 10225 8月  24 17:32 popen
-rwxr--r-- 1 wanglinbin wanglinbin   671 8月  24 17:32 popen.c
-rw-r--r-- 1 wanglinbin wanglinbin     0 8月  24 17:33 test_popen.txt
wanglinbin@SDserver:~/test/C-shell$ ls


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

阅读(858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~