问题
My intent was to have all the output of my bash script displayed on the console and logged to a file.
Here is my script that works as expected.
#!/bin/bash
LOG_FILE="test_log.log"
touch $LOG_FILE
# output to console and to logfile
exec > >(tee $LOG_FILE) 2>&1
echo "Starting command ls"
ls -al
echo "End of script"
However I do not understand why it works that way.
I expected to have exec >>(tee $LOG_FILE) 2>&1
work but it fails although exec >>$LOG_FILE 2>&1
indeed works.
I could not find the reason for the construction exec > >(command )
in the bash manual nor in advanced bash scripting. Can you explain the logic behind it ?
回答1:
The >(tee $LOG_FILE)
is an example of Process substitution, you might wish to search for that. Advanced Shell Scriptng and Bash manual
Using the syntax, <(program)
for capturing output and >(program)
for feeding input, we can pass data just one record at a time. It is more powerful than command substitution (backticks, or $( )
) because it substitutes for a filename, not text. Therefore anywhere a file is normally specified we can substitute a program's standard output or input (although process substitution on input is not all that common).
This is particularly useful where a program does not use standard streams for what you want.
Note that in your example you are missing a space, exec >>(tee $LOG_FILE) 2>&1
is wrong (you will get a syntax error),
exec > >(tee $LOG_FILE) 2>&1
is correct, that space is critical.
So, the exec >
part changes file descriptor 1 (the default), also known as stdout
or standard output, to refer to "whatever comes next", in this case it is the process substitution, although normally it would be a filename.
2>&1
redirect file descriptor 2, stderr
or standard error to refer to the same place as file descriptor 1 (if you omit the &
you end-up with a file called 1
).
Once you have done that, then you have changed the current process's standard output, so output from the commands which follow go to that tee
process.
来源:https://stackoverflow.com/questions/49509264/explain-the-bash-command-exec-tee-log-file-21