我有一個巨大的bash腳本,我想將特定的代碼塊記錄到特定的&小日誌文件(而不是一個巨大的日誌文件)。記錄代碼塊以便在bash中記錄文件
我有以下兩種方法:
# in this case, 'log' is a bash function
# Using code block & piping
{
# ... bash code ...
} | log "file name"
# Using Process Substitution
log "file name" < <(
# ... bash code ...
)
這兩種方法都可以與bash腳本的正確執行,例如干涉將值分配給變量時(如提出的問題here)。
您如何建議將日誌文件的輸出記錄到日誌中?
編輯: 這就是我試圖做(除了許多其他的變化),但並不如預期的工作:
function log()
{
if [ -z "$counter" ]; then
counter=1
echo "" >> "./General_Log_File" # Create the summary log file
else
((++counter))
fi
echo "" > "./${counter}_log_file" # Create specific log file
# Display text-to-be-logged on screen & add it to the summary log file
# & write text-to-be-logged to it's corresponding log file
exec 1> >(tee "./${counter}_log_file" | tee -a "./General_Log_File") 2>&1
}
log # Logs the following code block
{
# ... Many bash commands ...
}
log # Logs the following code block
{
# ... Many bash commands ...
}
執行的結果有所不同:有時日誌文件被創建,有時它們不會(會引發錯誤)。
請看看我的編輯。 – Dor
+1爲您的幫助 – Dor