【问题标题】:Redirect all messages to log file from inside script except some print command output on console as well as screen将所有消息从脚本内部重定向到日志文件,除了控制台和屏幕上的一些打印命令输出
【发布时间】:2020-11-05 03:26:42
【问题描述】:

我想默认将脚本的所有标准输出写入日志文件,但也想在控制台上显示一些消息。下面是我试过的示例代码。

#!/bin/bash
LOG='output.txt'
exec 3>&1
exec 1>>$LOG
exec 2>>$LOG
echo "This should be written to log"
echo "This should be written to both screen as well as log"|tee -a >&3

这会将This should be written to log 写入日志文件,这很好。

它只在控制台上写入This should be written to both screen as well as log,但我希望它同时写入控制台和日志。

【问题讨论】:

    标签: bash stdout


    【解决方案1】:

    您的 tee 命令缺少文件名。试试这样:

    echo "This should be written to both screen as well as log"|tee -a $LOG >&3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2011-07-16
      • 1970-01-01
      • 2021-08-17
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多