named-pipes - Bash 进程替换的行为与命名管道不同?
问题描述
有点跟进这个问题。我使用 bash 进程替换期望看到 5 行输出(对应于 5s 超时)但是我只看到 4 行:
timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(tr '\r' '\n' >&2)
[5.42kiB/s]
[1.89kiB/s]
[5.36kiB/s]
[2.41kiB/s]
但是,如果我在中间放置一个命名管道,我会按预期得到 5 行。第一个外壳:
mkfifo testfifo
cat testfifo | tr '\r' '\n' >&2
第二个外壳:
timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> testfifo
第一个 shell 的输出(5 行):
[3.79kiB/s]
[3.12kiB/s]
[ 4.4kiB/s]
[2.05kiB/s]
[5.58kiB/s]
为什么我在使用 bash 进程替换时没有得到 5 行?
奖金
我还尝试将输出转储到文件而不是stderr但这给了我一个空的日志文件:
timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(tr '\r' '\n' > rates.log)
我发现在使用stdbuf时,我的rates.log
文件包含最初预期的多行输出:
timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(stdbuf -oL tr '\r' '\n' > rates.log)
为什么我的tr需要 stdbuf ?(另请注意该文件包含 4 行,因此此调用结构可能会遇到与上述相同的问题)
解决方案
推荐阅读
- python - Python- %s %f %d % 等
- spring-integration - 如何处理在传输中终止的集成流
- python - “pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用”
- aws-lambda - 使用 Postman 测试 AWS Cognito 登录
- c# - 使用资源字典合并 xaml 文件
- laravel-5 - Laravel 5.4 路由简化
- python - 将 python 字符串列表转换为字节码以写入文件
- vert.x - 将请求主体反序列化为特定类而不是 JsonObject
- python - 'int' 对象不可调用?
- javascript - React Native AlertIOS 占位符