這裏是我的腳本文件: -如何使用不同的文件名進行登錄輸出和錯誤在不同的shell腳本文件
main.sh
# Log stdout and stderr
log_stdout_and_stderr() {
# Close STDOUT file descriptor
exec 1<&-
# Close STDERR FD
exec 2<&-
# Open STDOUT as $1 file for read and write.
exec 1<>$1
# Redirect STDERR to STDOUT
exec 2>&1
}
Log a single line
log() {
echo "[$(date)]: $*"
}
log_stdout_and_stderr main.log
log "Started main.sh"
log "Completed main.sh"
# call first_script
source first_script.sh
first_script.sh
log_stdout_and_stderr first_script.log
log "Started first_script.sh"
# call second_script
source second_script.sh
log "Completed first_script.sh"
second_script .sh
log_stdout_and_stderr second_script.log
log "Started second_script.sh"
log "Completed second_script.sh"
下面是我的輸出日誌: -
main.log
first_script.log
Started first_script.sh
second_script.log
Started second_script.sh
Completed second_script.sh
Completed first_script.sh
我想日誌消息應記錄如下面的日誌文件。
預期輸出: -
main.log
first_script.log
Started first_script.sh
Completed first_script.sh
second_script.log
Started second_script.sh
Completed second_script.sh
我打電話second_script.sh文件從first_script.s H。我想在second_script.log中存儲second_script.sh使用的日誌消息
我該怎麼辦?如果有人不清楚這個問題,請告訴我。
只需在該行'source second_script.sh'後面的'first_script.sh'中添加'log_stdout_and_stderr first_script.log'行即可。 – Ardit