作者:挥霍人生e | 来源:互联网 | 2022-12-11 05:42
我tee
在tee
信息页面中找到了一个使用实用程序的示例:
wget -O - http://example.com/dvd.iso | tee >(sha1sum > dvd.sha1) > dvd.iso
我查了一下>(...)
语法,找到了一个叫做"进程替换"的东西.根据我的理解,它使一个进程看起来像另一个进程可以写入/附加其输出的文件.(如果我在这一点上错了,请纠正我.)
这与管道有什么不同?(|
)我看到上面的例子中正在使用管道 - 它只是一个优先问题吗?或者还有其他一些区别吗?
1> that other g..:
这里没有任何好处,因为这条线也可以像这样编写:
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
与重定向有一个重要区别:它不重定向任何东西,而是将管道的名称(实际上类似于"/ dev/fd/63")作为参数传递给命令,并期望命令打开"文件"并读/写它.&#39;echo <(date)`示例@thatotherguy给出的应该显示这个,因为它只打印文件名而不是从中读取.
是。`echo <(date)`和`cat <(date)`提供了很好的线索。