我知道zsh的能力:
ls -1 >foo >bar
但是,假设我想通过另一个命令运行两个输出.例如,我如何组合这两个命令以避免两次运行mysqldump?
mysqldump db1 | bzip2 > db1.sql.bz2
mysqldump db1 | mysql db2
我能想到的最接近的是:
mysqldump db1 >db1.sql | mysql db2
bzip2 db1.sql
但我宁愿不将文件写入未压缩的磁盘(它很大!).
以下作品:
echo abc > >(gzip > 1) > >(xz > 2)
为您的示例修改(但未经测试):
mysqldump db1 > >(bzip2 > db1.sql.bz2) > >(mysql db2)
或者,可能更好:
{ mysqldump db1 } > >(bzip2 > db1.sql.bz2) > >(mysql db2)
//我在man zshexpn的PROCESS SUBSTITUTION部分找到了几乎相同的例子:
Also note that the previous example can be more compactly and efficiently written (provided the MULTIOS option is set) as:
06003
The shell uses pipes instead of FIFOs to implement the latter two process substitutions in the above example.
There is an additional problem with >(process); when this is attached to an external command,the parent shell does not wait for process to finish and hence an immediately following command cannot rely on the results being complete. The problem and solution are the same as described in the section MULTIOS in zshmisc(1). Hence in a simplified version of the example above:
06004
(note that no MULTIOS are involved),process will be run asynchronously. The workaround is:
06005
The extra processes here are spawned from the parent shell which will wait for their completion.