写终端标准输出重定向到一个文件,而无需使用标准错误后? [英] Write to terminal after redirecting stdout to a file without using stderr?

查看:140
本文介绍了写终端标准输出重定向到一个文件,而无需使用标准错误后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个shell脚本,一个作为主体的节目,另一个作为库。

I have two shell scripts, one that serves as the main "program" and another that serves as a "library."

在节目,我会做一些这样几个地方:日志$事>> $日志文件,其中日志是定义的函数库中。

In several places in the "program," I'll do something like: log "$thing" >> "$logfile", where log is a function defined in the "library."

# program.sh

logfile="log.txt"
stuff="hahah heheh hoho"

. library.sh 

for thing in $stuff; do
  log "$thing" >> "$logfile"
done

我的问题:有没有办法来重定向的部分的从函数的输出回到码头的没有的使用标准错误

My question: Is there a way to redirect some of the output from the function back to the terminal without using 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

}

我想避免,因为在我的实际程序中使用标准错误的,有错误重定向到一个文件的选项,但邮件我要发送到终端是信息,不是错误的,应该始终显示在终端上。

I want to avoid the use of stderr because in my actual program, there's an option to redirect errors to a file, but the messages I want to send to the terminal are informational, not errors, and should always show up on the terminal.

推荐答案

打开的/ dev / tty的另一个FD。

exec 0< /dev/null
exec 1> /dev/null
exec 2> /dev/null
exec 3> /dev/tty
echo 'Hello, World!' >&3 

这篇关于写终端标准输出重定向到一个文件,而无需使用标准错误后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆