gpt4 book ai didi

linux - linux shell 中的管道管理

转载 作者:IT王子 更新时间:2023-10-29 01:25:34 24 4
gpt4 key购买 nike

我目前正在研究如何将流水线管理到 shell 中。例如,在我的 shell 中,如果我输入“ls | wc | less”。此操作的结果将是创建三个进程,ls wc 和 less。ls 的输出将通过管道传输到 wc 的输入输入,而 wc 的输出将通过管道传输到 less 的输入输入。

对我来说,这意味着在执行“ls | wc | less”期间。 less 的标准输入不会是键盘,而是 wc 的输出。但是,less 仍然会响应我的键盘。为什么 ?我不明白,因为对我来说,less 不应该对键盘敏感,因为它已经通过管道传输了。

有人有想法吗?谢谢

最佳答案

来自less的代码

#if HAVE_DUP
/*
* Force standard input to be the user's terminal
* (the normal standard input), even if less's standard input
* is coming from a pipe.
*/
inp = dup(0);
close(0);
#if OS2
/* The __open() system call translates "/dev/tty" to "con". */
if (__open("/dev/tty", OPEN_READ) < 0)
#else
if (open("/dev/tty", OPEN_READ) < 0)
#endif
dup(inp);
#endif

它打开来自/dev/tty 的直接流以及任何你的标准输入。

关于linux - linux shell 中的管道管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46930395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com