Unix shell, redirect output but save stdin

I want to have a shell script redirect stdout of a child process as follows

  • Redirect stdout to file
  • Display process output in real time

I know I can do something like

#!/bin/sh ./child > file cat file 

But it does not display stdout in real time. For example, if the child was

 #!/bin/sh echo 1 sleep 1 echo 2 

The user will see both "1" and "2"

+4
source share
2 answers

Use tee :

 ./child | tee file 

tee will copy its standard input to any file on the command line and to standard output.

+14
source

I use: sponge, from moreutils http://linux.die.net/man/1/sponge

you can do something like this:

$ grep something largefile | sponge largefile

-1
source

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


All Articles