相关疑难解决方法(0)

在不使用stderr的情况下将stdout重定向到文件后写入终端?

我有两个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
Run Code Online (Sandbox Code Playgroud)

我的问题:有没有办法将函数的一些输出重定向回终端而不使用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

}
Run Code Online (Sandbox Code Playgroud)

我想避免使用stderr因为在我的实际程序中,有一个选项可以将错误重定向到文件,但是我想要发送到终端的消息是信息性的,而不是错误,应该始终显示在终端上.

bash shell redirect stdout

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

bash ×1

redirect ×1

shell ×1

stdout ×1