At the beginning of my script I have the "set -x" line. This, of course, sends all output to the screen. This is exactly what I like so that I can see what the script is doing and fix issues along the way. Once the script works correctly I comment out the "set -x" line.
I would like to tee the output of set -x so that I have a file to look at instead of using the display only. Is there a way to do this?
I've tried "set -x | tee ScriptNameHere.DEBUG" and it gives me a file named ScriptNameHere.DEBUG with ZERO bytes in it... an empty file.
set -x | tee scriptname.debug set -x | tee -a scriptname.debug
All give me a Zero byte file named scriptname.debug.
You can't do this with sh, but you can do it with bash (as long as you aren't on the ancient version shipped by Apple).
Explaining the above:
execis passed only redirections, and not a command, it performs those redirections in the current shell.BASH_XTRACEFDgives the number of the file descriptor where xtrace (set -x) will write logs.{varname}>filedynamically assigns a file descriptor number to the variable"$varname"of a handle writing to filefile.>(...)is a process substitution, replaced with a filename (typically on Linux something like/dev/fd/##or/proc/self/fd/##) that, when written to, will deliver content to the program invoked by the command...; one for read is instead<(...).Thus, the net effect is to start a copy of
teein the background and attach it to a file descriptor whose number is written to$BASH_XTRACEFD, such that whenset -xis later run, the logs will be written to that copy ofteeand thereafter to the specified file.