Suppose a shell script with the general form
(
# ... lots of code here
) > logfile 2>&1
There is a feature request to optionally have the logs dumped to the terminal as well as the file, which would be written
(
# ... lots of code here
) 2>&1 | tee logfile
The question is, how do I switch between these two pipeline constructs based on the value of a shell variable? Ideally without requiring a cat process, or anything like that, in the case where the logs are not going to the terminal, for efficiency.
It is a hard requirement to use POSIX shell features only. Also, due to bugs in older versions of zsh, the code inside the parentheses cannot be moved into a shell function.