-1

I have a .sh script on a red hat machine:

{
statement 1
statement 2
statement 3
etc....
} 2>&1 | tee "logdir/logfile.log"

which logs both to the console and into logfile logfile.com. However, statement 2 is giving a lot of output and I wish to exclude that from the logfile, while still showing output in the console.

Is there a way to exclude a specific statement from the tee to the logfile?

EDIT: A bit of clarification on the statements:

{
echo some information on the environment
echo some checks

rsync  -rtpgov /some/folder/ some/other/folder

tar -xf some/other/tar/file.tar  -C some/other/folder2
tar -xf some/other/tar/file.tar  -C some/other/folder3
tar -xf some/other/tar/file.tar  -C some/other/folder4
etc...

echo  some finishing checks
} 2>&1 | tee "logdir/logfile.log"

I wish to exclude the rsync from writing to the logfile.log while still seeing the output in the console.

Freeze
  • 237
  • 1
  • 5
  • 12
  • Could you show a bit more of your code before the statement? Could you put a condition, if (statement 2) before or are the statements only existing inside your function ? – night-gold Oct 05 '18 at 11:41

2 Answers2

0

Once tee is processing, can you see what lines should be excluded?

When you only know this when you are processing statement 2, it looks like you need to change each statement.

rm "logdir/logfile.log"
{
   echo 1 2>&1 | tee "logdir/logfile.log"
   echo 2 2>&1
   echo 3 2>&1 | tee -a "logdir/logfile.log"
} 2>&1
echo ===
echo "Contents File "logdir/logfile.log"
cat "logdir/logfile.log"

When statement 2 shows different kind of output, you can focus on that

rm "logdir/logfile.log"
{
   echo 1 useful
   echo 2 debug
   echo 3 this I want
} 2>&1 | tee >(grep -Ev "debug|trace|other garbage" > "logdir/logfile.log")
echo ===
echo "File "logdir/logfile.log":"
cat "logdir/logfile.log"

When you do not know what statement 2 will say, but want to filter this one without changing the other lines, you can make a workaround:

rm "logdir/logfile.log"
{
   echo 1 useful
   echo 2 debug | sed 's/^/FREEZE/'
   echo 3 this I want
} 2>&1 | tee >(grep "^FREEZE" > "logdir/logfile.log") | sed 's/^FREEZE//'
echo ===
echo "File "logdir/logfile.log":"
cat "logdir/logfile.log"
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

I have found a simple workable solution myself:

{
echo some information on the environment
echo some checks
# etc....
} 2>&1 | tee "logdir/logfile.log"    

{
rsync  -rtpgov /some/folder/ some/other/folder
} 2>&1 | tee "logdir/rsync.log"

{
tar -xf some/other/tar/file.tar  -C some/other/folder2
tar -xf some/other/tar/file.tar  -C some/other/folder3
tar -xf some/other/tar/file.tar  -C some/other/folder4
# etc...

echo  some finishing checks
} 2>&1 | tee -a "logdir/logfile.log"

Since the number of statements to log is relatively large and the input into the logfiles varies a lot it seems more convenient to split the logflie into 2. Using the -a option the loglines will be appended to the logfile

Freeze
  • 237
  • 1
  • 5
  • 12