【发布时间】:2011-07-11 20:09:54
【问题描述】:
我有一个巨大的 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 ...
}
执行结果各不相同:有时会创建日志文件,有时则不会(这会引发错误)。
【问题讨论】: