cedivad
cedivad

Reputation: 2644

Redirect command output to file - only when stdout has been closed from the command

Title should explain it all. I have a program whose stdout takes a lot to complete - i want to completely redirect the output to a file, but only when the output is complete and the command is closed. How can i do that?

Many thanks!

Upvotes: 0

Views: 1066

Answers (2)

jordanm
jordanm

Reputation: 34914

To do this, you need to store the output in a variable. I do not recommend this if the output is very large.

output=$(command)
printf "$output" > logfile

Upvotes: 1

Jonas Schäfer
Jonas Schäfer

Reputation: 20718

If you are aiming to have the output both on the terminal and in the file:

NAME
       tee - read from standard input and write to standard output and files

SYNOPSIS
       tee [OPTION]... [FILE]...

DESCRIPTION
       Copy standard input to each FILE, and also to standard output.

(from man tee)

Upvotes: 0

Related Questions