巴什匿名管道 [英] Bash Anonymous Pipes

查看:193
本文介绍了巴什匿名管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计的命令链,以执行特定的任务,我遇到了那个匿名管道不表现得像预期的问题。作为我运行的原始命令太复杂,在这里解释,我已经创建了一个显示问题(我知道,所有这些命令基本上什么都不做)的例子。此外,我使用的PV显示数据是否实际上从输入复制到输出。

When designing a chain of commands to perform a certain task, i ran into the problem that anonymous pipes do not behave like expected. As the original command that i am running is too complex to explain here, i've created an example that shows the problem (i know that all these commands are doing basically nothing). Also, i am using pv to show whether the data is actually copied from input to output.

cat /dev/zero | pv > /dev/null

这正常工作。 (从复制数据的/ dev /零到/ dev / null的)

This works as expected. (copy data from /dev/zero to /dev/null)

cat /dev/zero | tee /dev/null | pv > /dev/null

这也按预期工作(重复数据并将其发送拷贝到/ dev / null的)

This also works as expected (duplicate the data and send both copies to /dev/null)

cat /dev/zero | tee >(pv -c > /dev/null) | pv -c > /dev/null

这个命令只是部分有效。而从标准输入拷贝到标准输出仍然有效,(一个PV将显示很短的时间进度),整个命令得到由匿名管道,它不接受任何东西,因此发球摊位的输出中的一个不能被写入停滞(我通过让其写入文件,而不是的/ dev / null的检查这一点)。

This command only partially works. While the copy from STDIN to STDOUT still works, (one pv will show progress for a short time), the whole command gets stalled by the anonymous pipe, which does not receive anything and thus tee stalls as one of the outputs cannot be written to (I checked this by letting it write to files instead of /dev/null).

如果任何人有一个想法,为什么这并不在bash工作(如预期?),我会很高兴的帮助。

If anyone has an Idea why this does not work (as expected ?) in bash, i'd be glad for the help.

PS:如果我使用的zsh代替的bash,如预期运行该命令。不幸的是,这需要运行在系统中没有的zsh,也没有办法,我让该系统部署在zsh中。

PS: If I use zsh instead of bash, the command runs as expected. Unfortunately, the system this needs to run on has no zsh and there is no way for me to get zsh on that system deployed.

推荐答案

磨片使用≤(...)过程替代,这个过程中运行不有一个控制终端。但光伏总是显示其结果给终端;如果没有任何,它就会停止。

Whe you use <( ... ) for process substitution, the process running inside does not have a controlling terminal. But pv always shows its results to the terminal; if there isn't any, it gets stopped.

如果您执行code和,在运行时,做了 PS AXF ,你会看到这样的事情:

If you execute your code and, while it is running, do a ps axf, you will see something like this:

23412 pts/16   S      0:00  \_ bash
24255 pts/16   S+     0:00      \_ cat /dev/zero
24256 pts/16   S+     0:00      \_ tee /dev/fd/63
24258 pts/16   S      0:00      |   \_ bash
24259 pts/16   T      0:00      |       \_ pv -c
24257 pts/16   S+     0:00      \_ pv -c

......它告诉你,光伏-c 的进程替换(下面的第二个一内部执行庆典)是 T 状态的停止的。它正在等待有一个控制终端才能运行。它没有任何,所以它会永远停止,庆典最终停止发送数据到该管道。

...which tells you that the pv -c executed inside the process substitution (the one below the second bash) is in T state, stopped. It is waiting to have a controlling terminal in order to run. It does not have any, so it will stop forever, and bash eventually stops sending data to that pipe.

这篇关于巴什匿名管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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