用管道输送一个命令的输出作为另一命令的输入 [英] Piping the output of one command as input to another command

查看:107
本文介绍了用管道输送一个命令的输出作为另一命令的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我至少知道管道的基本知识.但是,我不明白如何使用C管道在C中实现此任务.我不知道如何将一个程序的输出作为另一程序的输入,依此类推.例如:

ls | wc | ./add

ls列出文件,wc列出列出的文件,./add加上wc给出的数字.

请帮助!

EDIT :这是一项作业.确切的问题陈述为:

编写一个C程序以读取两个(或更多)可执行文件的名称 程序,然后将第一个程序的输出重定向到 第二个程序,第二个程序的输出到第二个程序的输入 第三个程序,依此类推..."

解决方案

看来,您需要创建一个程序来完成Shell工作的简单情况:创建并执行命令管道,然后输出结果.

要实现此权利,您需要了解SIGPIPE,子进程处理,输入/输出重定向,文件描述符,fork()exec()wait()等.

这篇有关创建管道的Linux文档项目文章应该可以帮助您找到正确的路径

I know at least the basics of piping. However, I don't understand how to implement this task in in C using C pipes. I don't know how to take the output of one program as an input to another program and so on. Eg:

ls | wc | ./add

Here ls list the files, wc gives the counts of the listed files, and ./add adds the numbers given by wc.

Please help!

EDIT: It is an assignment. The exact problem statement is given as:

"Write a C program to read the names of two (or more) executable programs, and redirect the output of the first program to the input of the second program, output of the second program to the input of the third program, and so on..."

解决方案

It appears that you need to create a program that does a simple case of the shell's job: creates and executes a pipeline of commands then outputs the result.

To do this right requires you to understand SIGPIPE, child process handling, input/output redirection, file descriptors, fork() and exec(), wait(), and more.

This Linux Documentation Project article on creating pipelines should help set you on the right path.

这篇关于用管道输送一个命令的输出作为另一命令的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆