Redirect to stdout in bash -
is there filename assignable variable (i.e. not magic builtin shell token &1
) let me redirect stdout
?
what want run in cron script:
log=/tmp/some_file ... some_command 2>&1 >> $log echo "blah" >> $log ...
conveniently, lets me turn off log noise redirecting /dev/null
later when i'm sure there nothing can fail (or, @ least, nothing care about!) without rewriting whole script. yes, turning off logging isn't precisely best practice -- once script works, there not can conceivably go wrong, , trashing disk megabytes of log info nobody wants read isn't desired.
in case unexpectedly fails 5 years later, still possible turn on logging again flipping switch.
on other hand, while writing , debugging script, involves calling manually shell, extremely nice if dump output console. way wouldn't need tail
logfile manually.
in other words, i'm looking /proc/self/fd/0
in bash-talk can assign log
. happens, /proc/self/fd/0
works fine on linux box, wonder if there isn't such thing built bash (which preferrable).
basic solution:
#!/bin/bash log=/dev/null # uncomment next line debugging (logging) # log=/tmp/some_file { some_command echo "blah" } | tee 1>$log 2>&1
more evolved:
#!/bin/bash enable_log=0 # 1 log standard & error outputs log=/tmp/some_file { some_command echo "blah" } | if (( $enable_log )) tee 1>$log 2>&1 fi
more elegant solution devsolar's idea:
#!/bin/bash # uncomment next line debugging (logging) # exec 1> >(tee /tmp/some_file) 2>&1 some_command echo "blah"
Comments
Post a Comment