加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

将stdout传递给多个进程[zsh]

发布时间:2020-12-14 01:48:13 所属栏目:Linux 来源:网络整理
导读:我知道zsh的能力: ls -1 foo bar 但是,假设我想通过另一个命令运行两个输出.例如,我如何组合这两个命令以避免两次运行mysqldump? mysqldump db1 | bzip2 db1.sql.bz2mysqldump db1 | mysql db2 我能想到的最接近的是: mysqldump db1 db1.sql | mysql db2b
我知道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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读