【问题标题】:Redirect echo output in shell script to logfile将 shell 脚本中的 echo 输出重定向到日志文件
【发布时间】:2014-11-08 03:09:15
【问题描述】:

我有一个 shell 脚本,里面有很多 echo。我想将输出重定向到日志文件。 我知道有命令调用cmd > logfile.txt,或者在文件echo 'xy' > logfile.txt 中执行它,但是是否可以简单地在脚本中设置文件名,然后自动将所有回声写入该文件?

【问题讨论】:

  • 您要 echos 重定向吗?还有其他产生输出的命令吗?
  • 除了 echo 没有其他输出
  • 我认为这是我赞成该问题的第一个问题加上 3 个答案。都好。干得好,社区!

标签: shell stdout


【解决方案1】:

您可以在脚本顶部添加这一行:

#!/bin/bash
# redirect stdout/stderr to a file
exec >logfile.txt 2>&1

否则只重定向标准输出使用:

exec > logfile.txt

【讨论】:

  • 我想写入登录文件并希望它出现在控制台上。你能指导一下我应该用什么。
  • 我试过这个exec > myLog.log 2>&1 | tee myLog.log 它显示在控制台上,但没有写入文件。我试过command | tee file.log,但这也只显示在控制台上。
  • 它只在文件中写入系统日期
  • 这就是我们使用的命令的所有输出。好的,最好不要在不相关的问题上延长聊天会话。
  • Bashism &> 在这里是非常免费的;可移植符号>logfile.txt 2>&1 更明确,但恕我直言,更具可读性和可移植性(然后很容易更改为>>,而&>> 是最近才引入的,并且在仍然只提供Bash 的平台上不可用3,比如macOS)。
【解决方案2】:

我尝试使用以下命令进行管理。这会将输出写入日志文件并在控制台上打印。

#!/bin/bash

# Log Location on Server.
LOG_LOCATION=/home/user/scripts/logs
exec > >(tee -i $LOG_LOCATION/MylogFile.log)
exec 2>&1

echo "Log Location should be: [ $LOG_LOCATION ]"

请注意:这是 bash 代码,所以如果你使用 sh 运行它会出现语法错误

【讨论】:

  • 那么我如何追加而不是覆盖?还是因为我们也在写屏幕所以不能这样做? (我尝试将行修改为“exec >> >(tee..”但它仍然会覆盖)
  • @MarkSmith 请与-a 选项一起使用:exec > >(tee -a $log)
  • 当然!我有点知道 tee 的 -a 选项,但完全忘记了。谢谢?
  • 以上工作完美;但好奇可以再解释这是如何/为什么起作用的吗?
  • @Kelly 符号>(cmd) 实质上创建了一个并行进程,该进程在包含此进程替换的命令期间运行cmd,其输出重定向到cmd 的标准输入。命令exec 是这里的特例;它的唯一目的是创建一个进程替换,直到脚本结束,或者下一个 exec 将相同的文件描述符重定向到不同的目标。
【解决方案3】:

您可以使用子 shell 轻松地将 shell 脚本的不同部分重定向到一个文件(或多个文件):

{
  command1
  command2
  command3
  command4
} > file1
{
  command5
  command6
  command7
  command8
} > file2

【讨论】:

    【解决方案4】:
    LOG_LOCATION="/path/to/logs"    
    exec >> $LOG_LOCATION/mylogfile.log 2>&1
    

    【讨论】:

      【解决方案5】:
      #!/bin/sh
      # http://www.tldp.org/LDP/abs/html/io-redirection.html
      echo "Hello World"
      exec > script.log 2>&1
      echo "Start logging out from here to a file"
      bad command
      echo "End logging out from here to a file"
      exec > /dev/tty 2>&1 #redirects out to controlling terminal
      echo "Logged in the terminal"
      

      输出:

      > ./above_script.sh                                                                
      Hello World
      Not logged in the file
      > cat script.log
      Start logging out from here to a file
      ./logging_sample.sh: line 6: bad: command not found
      End logging out from here to a file
      

      在此处阅读更多信息:http://www.tldp.org/LDP/abs/html/io-redirection.html

      【讨论】:

      • ABS 是一个相当可疑的参考;链接到官方 Bash 参考手册或 POSIX 可能会更好。
      【解决方案6】:

      在控制台上获取输出并将输出记录到文件中:

      script.sh

      #!/bin/bash
      (
        #Command 1
        #Command 2
        #Command 3
        ...
      ) 2>&1 | tee /path/to/save/console_output.log
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-21
        • 2022-09-23
        • 1970-01-01
        • 2020-05-28
        • 1970-01-01
        • 2019-08-21
        • 2012-10-26
        • 2020-02-11
        相关资源
        最近更新 更多