How can I gzip standard in to a file and also print standard in to standard out?

前端 未结 4 1116
天命终不由人
天命终不由人 2020-12-14 06:28

I want to execute a command, have the output of that command get gzip\'d on the fly, and also echo/tee out the output of that command.

i.e., something like:

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 06:57

    Have a nice cup of tee!

    The tee command copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy

    As I'm having a slow afternoon, here's some gloriously illustrative ascii-art...

               +-----+                   +---+                  +-----+  
    stdin ->   |cmd 1|    -> stdout ->   |tee|   ->  stdout  -> |cmd 2|
               +-----+                   +---+                  +-----+
                                           |
                                           v
                                         file
    

    As greyfade demonstrates in another answer the 'file' need not be a regular file, but could be FIFO letting you pipe that tee'd output into a third command.

               +-----+                   +---+                  +-----+  
    stdin ->   |cmd 1|    -> stdout ->   |tee|   ->  stdout  -> |cmd 2|
               +-----+                   +---+                  +-----+
                                           |
                                           v
                                         FIFO
                                           |
                                           v
                                        +-----+
                                        |cmd 3|
                                        +-----+
    

提交回复
热议问题