How can I split and re-join STDOUT from multiple processes?

前端 未结 3 974
南旧
南旧 2021-02-15 04:07

I am working on a pipeline that has a few branch points that subsequently merge-- they look something like this:

         command2
        /        \\
command1           


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-15 04:32

    Please see also https://unix.stackexchange.com/questions/28503/how-can-i-send-stdout-to-multiple-commands. Amongst all answers, I found this answer particularly fits my need.

    Expand a little bit @Soren's answer,

    $ ((date | tee >( wc >&3) | wc) 3>&1) | cat -n
         1         1       6      29
         2         1       6      29
    

    You can do without using tee but an environment variable,

    $ (z=$(date); (echo "$z"| wc ); (echo "$z"| wc) ) | cat -n
         1         1       6      29
         2         1       6      29
    

    In my case, I applied this technique and wrote a much complex script that runs under busybox.

提交回复
热议问题