【发布时间】:2012-03-13 09:28:48
【问题描述】:
我有两个 shell 脚本,一个用作主“程序”,另一个用作“库”。
在“程序”的几个地方,我将执行以下操作:log "$thing" >> "$logfile",其中log 是“库”中定义的函数。
# program.sh
logfile="log.txt"
stuff="hahah heheh hoho"
. library.sh
for thing in $stuff; do
log "$thing" >> "$logfile"
done
我的问题:有没有办法使用stderr 将一些函数的输出重定向回终端而不?
# library.sh
log () {
# This gets written to the log
echo "`date --rfc-3339=seconds`: $1"
# How to write this to the terminal *without* using stderr?
echo "Info: Message written to log." >&2
}
我想避免使用stderr,因为在我的实际程序中,可以选择将错误重定向到文件,但我要发送到终端的消息是信息性的,而不是错误的,并且应该始终显示在终端上。
【问题讨论】:
标签: bash shell redirect stdout