shell管道重定向程序的实现
gao.lu.c~
ls.o
pipedup
test1
test2
test3
test4
#include <unistd.h>
{
int f_des[2]; //f_des[0]:for read, f_des[1]:for write.
int pid;
{
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;
}
执行结果:
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$
#include <unistd.h>
{
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$ ./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管道重定向程序的实现相关推荐
- 7.18 Shell 管道 重定向 链接
SHell 基础知识 Shell是最常使用程序 其作用是侦听用户命令.启动命令所指定的程序并将结果返回给用户.shell和bash. B ...
- (管道| / 重定向 / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)
原文连接:http://www.cnblogs.com/alwayswyy/p/5191137.html 本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下 一. ...
- Linux脚本保存管道中的变量,Linux下管道重定向使用以及Shell编程(操作系统)
实验名称:Linux的基本操作 实验目的: 1.了解管道和重定向 2.熟悉基本的Linux脚本的编写 实验环境:Ubuntu 12.4(32位,简体中文) 实验内容: 1.将当前用户目录下的文件清单输 ...
- 《Unix/linux编程实践教程》------重定向程序的I/O
<Unix/linux编程实践教程>书中举例命令more的用法: $more filename $command | more $more < filename 用法1直接显示fil ...
- shell 输入输出重定向
1. 命令列表: command > file 将输出重定向到file command < file 将输入重定向到file command >> file 将输出以追加的方式 ...
- 1-6:学习shell之重定向
文章目录 一:标准输入,标准输出和标准错误 (1):标准输出重定向 A:`>`重定向 B:`>>`重定向 (2):标准错误重定向 (3):将标准输出和标准错误重定向到同一个文件中 ( ...
- Linux入门之inode解析及管道重定向
Linux入门之inode解析及管道重定向 inode 简介: 当磁盘分区格式化后会根据分区格式.大小等信息来指定分区分配多少个inode表,每个inode表都会有一个在当前分区中唯一的编号,可能有一 ...
- linux shell数据重定向(输入重定向与输出重定向)详细分析
转载自: linux shell数据重定向(输入重定向与输出重定向)详细分析 - 程默 - 博客园 http://www.cnblogs.com/chengmo/archive/2010/10/20/ ...
- 重定向程序无法决定链接类型 解决方案
症状 当您将网络驱动器映射到本地共享时,系统事件日志中可能会报告以下警告消息: 事件 ID: 3019 来源:MRxSmb 描述:重定向程序无法决定链接类型. 原因 当 TCP/IP 上的 NetBI ...
最新文章
- java解析各种文档格式_java读取各类型的文件
- 百度定位sdk使用说明
- 使用VNS 使用XWINDOWS 登录远程服务器方法
- Linux系统编程---4(进程间通信IPC,管道)
- 下岗职工_下岗后我如何获得多位软件工程师的面试
- 笔记:python设计模式
- pythonrequests证书_python requests证书问题解决
- 玩冒险岛java卸载_如何删除冒险岛安装了,现在不想玩
- Azkaban 安装
- Linux 学习步骤(从入门到精通)
- eclipse安装Windows Builder
- dell h330 不要做raid5 ,做raid0就可以了
- 移动硬盘格式避坑指南
- css幸运大转盘,用CSS实现一个抽奖转盘
- OpenCV 局部自适应对比度增强ACE算法
- 电大计算机试题及答案形成性,国开电大机械CAD/CAM形成性作业1答案
- 微信群发工具-含源代码分享
- 我的网站http://www.xhu.cn/xhu/520/
- Jquery(终极完整版)
- R语言dplyr包的select函数和everthing函数筛选dataframe中的所有数据列(all columns of the dataframe)