I'm trying to modify a script that uses the following:
# first portion of script
# ...
exec > >(tee -ia $OUT)
# ...
# second portion of script
The problem I have with this script is that it produces voluminous output to stdout
(my terminal). The script author included no options for eliminating the terminal output. I would like to add an option that removes the stdout
, and get the output solely in the file $OUT
.
Here's what I've tried:
TERM_OPT="OFF"
first portion of script
...
if [ $TERM_OPT != "OFF" ]; then
exec > >(tee -ia $OUT)
else {
...
second portion of script
} > $OUT
fi
This seems to work, but I'm unsure about the use of curly braces {}
in this context as the GNU secion of Grouping Commands (seems to) state that a semicolon ;
is required following the list. But adding a ;
or leaving it off seems to make no difference. I've wondered if I should use parentheses ()
instead of curly braces, but this causes everything inside the ()
to execute in a subshell. I'm not particularly keen on that as it's someone else's script & the subshell implications are unclear to me (I didn't try this).
The other thing I tried seemed like a hack, but I read of others using it, and it seems to work OK also:
TERM_OPT="OFF"
first portion of script
...
if [ $TERM_OPT != "OFF" ]; then
exec > >(tee -ia $OUT)
else
exec > >(tee -ia $OUT 1> /dev/null)
fi
...
second portion of script
I like this as it seems more self-contained, but that's not much of a consideration AFAICT.
So the Question is: What's the correct way to do this? By that, I mean what's the correct way to opt out of the terminal output after an exec > >(tee -ia $OUT)
? Is one of these solutions preferable to the other - or do I need to do something completely different?
--
? – Seamus Jan 08 '24 at 08:29--
mark the last option, and therefore anything following must be an argument? – Seamus Jan 08 '24 at 08:49$OUT
is variable so may very well start with-
like inOUT=---myfile---.log
– Stéphane Chazelas Jan 08 '24 at 09:06tee
?? By that I mean, iftee
has not finished writing the output file (due to a slow HDD maybe) shouldn't the shell keep the process open until it finishes? – Seamus Jan 08 '24 at 09:29tee
doesn't finish until it sees the end of its input. If the shell, whose stdout is redirected to the pipe to tee doesn't terminate then tee doesn't terminate so the shell would never stop if it was waiting for tee. You need to actively close that pipe like we do here withexec > /dev/null
fortee
to terminate (assuming there's no other background process with stdout or other fd redirected to that pipe). – Stéphane Chazelas Jan 08 '24 at 09:41