用管道输送一个命令的输出作为另一命令的输入 [英] Piping the output of one command as input to another command
问题描述
我至少知道管道的基本知识.但是,我不明白如何使用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屋!