加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 18:59:30 所属栏目:安全 来源:网络整理
导读:我有两个shell脚本,一个用作主“程序”,另一个用作“库”. 在“程序”的几个地方,我会做类似的事情:log“$thing” “$logfile”,其中log是“库”中定义的函数. # program.shlogfile="log.txt"stuff="hahah heheh hoho". library.sh for thing in $stuff; do
我有两个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,因为在我的实际程序中,有一个选项可以将错误重定向到文件,但是我想要发送到终端的消息是信息性的,而不是错误,应该始终显示在终端上.

在另一个FD上打开/ dev / tty.
exec 0< /dev/null
exec 1> /dev/null
exec 2> /dev/null
exec 3> /dev/tty
echo 'Hello,World!' >&3

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读