管道常用于不同命令的组合使用,比如ps -aux|grep ..用于查找指定条件的进程,ls|less用于实现文件列表的分页显示等。总之是将前一个命令的输出作为后一个命令的输入,完成一个命令无法完成的功能。
分别使用pipe()和popen()两种方式实现ls|less(ls|more)的功能。
gaolu@gaolu-desktop:~/test_folder$ ls |less(文件数目比较少,less和more显示出来一样)
gao.lu.c
gao.lu.c~
ls.o
pipedup
test1
test2
test3
test4
【1】使用pipe()实现
pipe()函数可以生成2个文件描述符,分别指向管道的read和write端,创建子进程,然后在父子进程内部分别执行ls和less命令,前者将标准输出重定向到管道,后者将标准输入重定向到管道。
#include <stdio.h>
#include <unistd.h>
int main(int argc,char* argv[])
{
 int f_des[2];  //f_des[0]:for read, f_des[1]:for write.
 int pid;
if(argc != 3)
 {
  printf("Usage: %s comand1,comand2.\n",argv[0]);
  return 1;
 }
 if(pipe(f_des)==-1)
 {
  perror("Fail to creat pipe.\n");
  return 1;
 }
 if((pid=fork())==-1)
 {
  perror("Fail to creat child process.\n");
  return 1;
 }
 if(pid==0)
 {
  
  dup2(f_des[0],STDIN_FILENO);
  close(f_des[0]);
  close(f_des[1]);
  if(execlp(argv[1],argv[1],NULL)==-1)
  {
   printf("Child process can't exec command %s.\n",argv[1]);
   return 1;
  }
  _exit(0);
  
  
 }
 else
 {
  
  dup2(f_des[1],STDOUT_FILENO);
  close(f_des[0]);
  close(f_des[1]);
  if(execlp(argv[2],argv[2],NULL)==-1)
  {
   printf("Parent process can't exec command %s.\n",argv[2]);
   return 1;
  }
  wait(NULL);
  _exit(0);
 }
 return 0;
}
执行结果:
gaolu@gaolu-desktop:~/test_folder$ ./pipedup
Usage: ./pipedup comand1,comand2.
gaolu@gaolu-desktop:~/test_folder$ ./pipedup ls less
gao.lu.c  gao.lu.c~  ls.o  pipedup  test1  test2  test3  test4
Missing filename ("less --help" for help)
gaolu@gaolu-desktop:~/test_folder$
【2】使用popen()实现
有warnning(fs貌似应该做个强制类型转换再dup么)。相当于在上面pipe()的实现中,让父进程调用通过execlp执行ls命令,将标准输出重定向到管道,而子进程从管道中去数据执行less打印到标准输出。
#include <stdio.h>
#include <unistd.h>
int main (int argc,char* argv[])
{
 if(argc != 3)
 {
  printf("Usage: %s comand1, comand2.\n",argv[0]);
  return 1;
 }
 FILE* fs = popen(argv[2],"w");
 if(NULL == fs)
 {
  perror("Call popen function failed.\n");
  return 1;
 }
 dup2(fs,STDOUT_FILENO);
 if(execlp(argv[1],NULL)==-1)
 {
  printf("Exec command %s failed.\n",argv[1]);
  return 1;
 }
 pclose(fs);
 exit(0);
}
执行结果:
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$ ./popen.o ls more
gao.lu.c  gao.lu.c~  ls.o  pipedup  popen.o  test1  test2  test3  test4
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$
gaolu@gaolu-desktop:~/test_folder$ ls|more
gao.lu.c
gao.lu.c~
ls.o
pipedup
popen.o
test1
test2
test3
test4
gaolu@gaolu-desktop:~/test_folder$

转载于:https://blog.51cto.com/keren/144930

shell管道重定向程序的实现相关推荐

  1. 7.18 Shell 管道 重定向 链接

                                   SHell 基础知识 Shell是最常使用程序 其作用是侦听用户命令.启动命令所指定的程序并将结果返回给用户.shell和bash. B ...

  2. (管道| / 重定向 / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)

    原文连接:http://www.cnblogs.com/alwayswyy/p/5191137.html 本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下 一. ...

  3. Linux脚本保存管道中的变量,Linux下管道重定向使用以及Shell编程(操作系统)

    实验名称:Linux的基本操作 实验目的: 1.了解管道和重定向 2.熟悉基本的Linux脚本的编写 实验环境:Ubuntu 12.4(32位,简体中文) 实验内容: 1.将当前用户目录下的文件清单输 ...

  4. 《Unix/linux编程实践教程》------重定向程序的I/O

    <Unix/linux编程实践教程>书中举例命令more的用法: $more filename $command | more $more < filename 用法1直接显示fil ...

  5. shell 输入输出重定向

    1. 命令列表: command > file 将输出重定向到file command < file 将输入重定向到file command >> file 将输出以追加的方式 ...

  6. 1-6:学习shell之重定向

    文章目录 一:标准输入,标准输出和标准错误 (1):标准输出重定向 A:`>`重定向 B:`>>`重定向 (2):标准错误重定向 (3):将标准输出和标准错误重定向到同一个文件中 ( ...

  7. Linux入门之inode解析及管道重定向

    Linux入门之inode解析及管道重定向 inode 简介: 当磁盘分区格式化后会根据分区格式.大小等信息来指定分区分配多少个inode表,每个inode表都会有一个在当前分区中唯一的编号,可能有一 ...

  8. linux shell数据重定向(输入重定向与输出重定向)详细分析

    转载自: linux shell数据重定向(输入重定向与输出重定向)详细分析 - 程默 - 博客园 http://www.cnblogs.com/chengmo/archive/2010/10/20/ ...

  9. 重定向程序无法决定链接类型 解决方案

    症状 当您将网络驱动器映射到本地共享时,系统事件日志中可能会报告以下警告消息: 事件 ID: 3019 来源:MRxSmb 描述:重定向程序无法决定链接类型. 原因 当 TCP/IP 上的 NetBI ...

最新文章

  1. java解析各种文档格式_java读取各类型的文件
  2. 百度定位sdk使用说明
  3. 使用VNS 使用XWINDOWS 登录远程服务器方法
  4. Linux系统编程---4(进程间通信IPC,管道)
  5. 下岗职工_下岗后我如何获得多位软件工程师的面试
  6. 笔记:python设计模式
  7. pythonrequests证书_python requests证书问题解决
  8. 玩冒险岛java卸载_如何删除冒险岛安装了,现在不想玩
  9. Azkaban 安装
  10. Linux 学习步骤(从入门到精通)
  11. eclipse安装Windows Builder
  12. dell h330 不要做raid5 ,做raid0就可以了
  13. 移动硬盘格式避坑指南
  14. css幸运大转盘,用CSS实现一个抽奖转盘
  15. OpenCV 局部自适应对比度增强ACE算法
  16. 电大计算机试题及答案形成性,国开电大机械CAD/CAM形成性作业1答案
  17. 微信群发工具-含源代码分享
  18. 我的网站http://www.xhu.cn/xhu/520/
  19. Jquery(终极完整版)
  20. R语言dplyr包的select函数和everthing函数筛选dataframe中的所有数据列(all columns of the dataframe)

热门文章

  1. composer(作曲家)安装php-ml
  2. 使用腾讯开发平台获取QQ用户数据资料
  3. VC++ 使用attributes定义接口
  4. Angular自学笔记(二)显示数据 绑定属性
  5. What Are You Talking About HDU1075
  6. Python自建collections模块
  7. 【NOIP】关押罪犯
  8. Codeforces 724 C. Ray Tracing
  9. AsyncTask与多任务
  10. ad network