How do I redirect stdout and/or stderr to a path I have specified in a variable? Note: I don't want to overwrite the variable itself, I want to make std[xxx] write to the file specified in the variable.
For example- a simple script that, if the scp
command fails, instead of printing the failure message to stderr
or stdout
(I'm not sure to which it outputs when fail), it outputs it to a specified log file. The path to the log file is stored in the $LOG
variable:
LOG=/path/to/file.log
scp file1 host@remote
# do 'whatever' if scp command succeeds:
if [ $? = 0 ];
then
whatever
else
# else log both stdout/stderr to ${LOG} file
&>"${LOG}"
# DEBUG - print contents of ${LOG} var for testing purposes
printf "${LOG}"
The result of this script doesn't show anything in the /path/to/file.log
file and simple prints /path/to/file.log
to stdout
. So it's as if nothing was written from &>
.
I've already confirmed my particular scp
command works, so I know that's not a potential issue.
Or is this even the most proper way to handle custom log files? Is there a better practice for configuring your own logging system than storing paths to log files in variables?
scp
is run, or with thescp
line, not after thescp
has already gone away.