简体   繁体   English

在C中处理管道以创建外壳

[英]Handling pipes in C to create a shell

cat file.txt | grep hello

The way I see this is that cat file.txt is executed and whatever is passed to the stdout is passed as input in a pipe to grep hello . 我看到的方式是执行cat file.txt并将传递到stdout的任何内容作为输入传递到grep hello的管道中。 Then grep hello finds all the occurrences that match hello in the given input. 然后grep hello在给定的输入中查找所有与hello匹配的事件。

Here is my confusion: does grep hello write back to the pipe so the parent process outputs whatever is in stdout? 这是我的困惑: grep hello写回管道,以便父进程输出stdout中的内容? Or does grep hello put its returned content in stdout? 还是grep hello将返回的内容放入stdout? How does it work? 它是如何工作的?

I am asking this question because I am writing a shell in C. 我问这个问题是因为我正在用C编写Shell。

cat 's stdout is connected to grep 's stdin. cat的stdout连接到grep的stdin。 grep 's stdout is connected to the default location, probably the terminal. grep的stdout连接到默认位置,可能是终端。 grep doesn't feed its output back to cat -- the pipe is unidirectional. grep不会将其输出反馈给cat管道是单向的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM