bash - 使用进程替换与管道有什么区别?
问题描述
我在信息页面中遇到了 usingtee
实用程序的示例:tee
wget -O - http://example.com/dvd.iso | tee >(sha1sum > dvd.sha1) > dvd.iso
我查了一下>(...)
语法,发现了一个叫做“进程替换”的东西。据我了解,它使一个进程看起来像另一个进程可以写入/附加其输出的文件。(如果我在这一点上错了,请纠正我。)
这与管道有何不同?( |
) 我看到在上面的例子中使用了一个管道——它只是一个优先级问题吗?还是有其他区别?
解决方案
这里没有任何好处,因为该行同样可以这样写:
wget -O - http://example.com/dvd.iso | tee dvd.iso | sha1sum > dvd.sha1
当您需要与多个程序进行管道传输时,差异开始出现,因为这些不能纯粹用|
. 随意尝试:
# Calculate 2+ checksums while also writing the file
wget -O - http://example.com/dvd.iso | tee >(sha1sum > dvd.sha1) >(md5sum > dvd.md5) > dvd.iso
# Accept input from two 'sort' processes at the same time
comm -12 <(sort file1) <(sort file2)
它们在您出于任何原因不能或不想使用管道的某些情况下也很有用:
# Start logging all error messages to file as well as disk
# Pipes don't work because bash doesn't support it in this context
exec 2> >(tee log.txt)
ls doesntexist
# Sum a column of numbers
# Pipes don't work because they create a subshell
sum=0
while IFS= read -r num; do (( sum+=num )); done < <(curl http://example.com/list.txt)
echo "$sum"
# apt-get something with a generated config file
# Pipes don't work because we want stdin available for user input
apt-get install -c <(sed -e "s/%USER%/$USER/g" template.conf) mysql-server
推荐阅读
- python - Django 网站部署错误:在 cPanel 中为 Pillow、Reportlab 构建轮子失败
- python - 桩的游戏
- javascript - Javascript中的拉格朗日插值多项式算法
- gtk - 在 Vala 中为 Gtk.Entry 连接 insert_text 信号
- objective-c - 访问在另一个类中定义的 IBOutlet id
- javascript - GET http://localhost:5000/user net::ERR_CONNECTION_REFUSED
- javascript - NPM/Grunt 编译器脚本,允许我导出到多个文件
- unity3d - Unity Ads 测试模式为 false,但每次都显示测试广告
- portforwarding - 使用 Webmin 进行端口转发
- python - TensorFlow 的自定义损失函数