Bring stdout to multiple processes [zsh]

I know about zsh feature:

ls -1 >foo >bar 

But let me say that I want to run both exits through another command. For example, how would you combine these two commands so as not to run mysqldump twice?

 mysqldump db1 | bzip2 > db1.sql.bz2 mysqldump db1 | mysql db2 

Closest I can come up with the following:

 mysqldump db1 >db1.sql | mysql db2 bzip2 db1.sql 

But I would prefer not to write the file to disk without compression (it's big!).

+4
source share
3 answers

The following works:

 echo abc > >(gzip > 1) > >(xz > 2) 

Changed for your example (but not verified):

 mysqldump db1 > >(bzip2 > db1.sql.bz2) > >(mysql db2) 

or perhaps better:

 { mysqldump db1 } > >(bzip2 > db1.sql.bz2) > >(mysql db2) 

// I found almost the same example in the REPLACEMENT PROCESS section in man zshexpn :

Also note that the previous example may be more compact and efficient (provided that the MULTIOS option is set):

  paste <(cut -f1 file1) <(cut -f3 file2) \ > >(process1) > >(process2) 

In the above example, the shell uses channels instead of FIFO to implement the last two permutations.

There is an additional problem s> (by process); when this is tied to an external command, the parent shell does not wait for the process to complete, and therefore the next command immediately cannot rely on the final results. The problem and solution are the same as described in the MULTIOS section in zshmisc (1). Therefore, in a simplified version of the above example:

  paste <(cut -f1 file1) <(cut -f3 file2) > >(process) 

(note that MULTIOS is not involved), the process will execute asynchronously. Workaround:

  { paste <(cut -f1 file1) <(cut -f3 file2) } > >(process) 

Additional processes here are generated from the parent shell, which will wait for them to complete.

+4
source

You can use process substitution.

In zsh:

 ls -1 > >(grep foo) > >(grep bar) 

In bash:

 ls -1 | tee >(grep foo) >(grep bar) >/dev/null 

Process substitution controls the channels you specify.

+5
source

You can connect processes through fifo and use the tee utility to copy standard output to each fifo and to stdout. Sort of:

 mkfifo db1.sql bzip2 db1.sql & mysqldump db1 | tee db1.sql | mysql db2 
+1
source

Source: https://habr.com/ru/post/1339697/


All Articles