0

I am trying to log the changes made by my script to a log file. At a very high level, my script has functions and I want to be able to log that information to a file. I used "tee -a" but that messed up the functionality in a number of ways.

Is there a simple way to achieve this task ?

Update : Corrected typo below

function1(){ ... }
function2(){ ... }
#main
function1 | tee -a /tmp/logfile
function2 | tee -a /tmp/logfile

1 Answer 1

1

(edited to reflect question edits) You can incorporate the tee into the function definition:

function() { { ...<original function definition goes here>; } | tee -a output; }

so you don't need to invoke tee each time you call the function. Obviously, if the function modifies file descriptors, you will need to do a little more work. Also, keep in mind that this changes the buffering. If commands called from within function1 have a tty for their stdout, they will probably line buffer their output, but if their stdout is a pipe (which it is if you are piping to tee) the output will be block buffered. This may be the root cause of the differences you are seeing. Also, this only captures the output of one file descriptor. Perhaps you have commands writing to stderr. You will need to provide more details about the way the pipe to tee changes the behavior of the script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.