Search code examples
bashoutput-redirect

Output of complete script to variable


I have rather complex bash script which is normally run manually and thus needs live output on the console (stdout and stderr). However, since the outcome and output of this script is rather important I'd like to save its output at the end into a database.

I have already a trap function for this and the database query as such is also not a problem. The problem is: How do I get the output of the entire script till that point into a variable?

The live console output should be preserved. The output after the database query (if any) does not matter.

Is this possible at all? Might it be necessary to wrap the script into another script (file)?


Solution

  • I'm doing similar task like this

    exec 5>&1
    message=$(check|tee /dev/fd/5)
    mutt -s "$subjct" "$mailto" <<< "$message"
    

    Add your script instead of check function and change mailing to db query.