Sample Header Ad - 728x90

Creating an option for 'exec > >(tee -ia $OUT)' to skip stdout

0 votes
2 answers
98 views
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](https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html) (*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?
Asked by Seamus (3772 rep)
Jan 8, 2024, 07:26 AM
Last activity: Jan 8, 2024, 08:20 AM