출력 사본을 원하면 다음과 같이 사용할 수 있습니다 tee.
#!/bin/bash
init
do_something_that_outputs_stuff_to_STDOUT | tee script.log
launch_applications_that_output_to_STDOUT | tee -a script.log
fini
그러나 이것은 stdout 만 script.log에 기록합니다. stderr 및 stdout이 모두 리디렉션되도록하려면 다음을 사용하십시오.
#!/bin/bash
init
do_something_that_outputs_stuff_to_STDOUT 2>&1 | tee script.log
launch_applications_that_output_to_STDOUT 2>&1 | tee -a script.log
fini
약간의 기능으로 조금 더 멋지게 만들 수도 있습니다.
#!/bin/bash
LOGFILE="script.log"
# Wipe LOGFILE if you don't want to add to it at each run
rm -f "$LOGFILE"
function logcmd() {
local cmd="$1" logfile=${LOGFILE:-script.log} # Use default value if unset earlier
# Launch command, redirect stderr to stdout and append to $logfile
eval '$cmd' 2>&1 | tee -a "$logfile"
}
init
logcmd "do_something_that_outputs_stuff_to_STDOUT"
logcmd "launch_applications_that_output_to_STDOUT"
fini