【问题标题】:Print on terminal and into file simultaneously?在终端上同时打印到文件中?
【发布时间】:2012-05-28 09:31:45
【问题描述】:

我有一个 greps 一些数据的 shell 脚本。我想将结果打印到一个文件中,但是这样做会阻止结果显示在终端上。有没有办法既可以在屏幕上打印结果又可以写入文件。 提前致谢。

【问题讨论】:

    标签: linux shell terminal


    【解决方案1】:

    将您的输出传递给tee 命令。

    例子:

    [me@home]$ echo hello | tee out.txt
    hello
    [me@home]$ cat out.txt 
    hello
    

    注意echo的stdout被打印出来并写入到thrtee命令指定的文件中。

    【讨论】:

      【解决方案2】:

      请注意,您可以将 -a 标志添加到 tee 以附加到输出文件中

      [me@home]$ echo hello | tee out.txt
      hello
      [me@home]$ echo hello again | tee -a out.txt
      hello again
      [me@home]$ cat out.txt
      hello
      hello again
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 2011-02-24
          • 2016-07-26
          • 2014-12-23
          • 1970-01-01
          • 2019-07-13
          • 1970-01-01
          • 2013-06-27
          • 2013-09-14
          • 1970-01-01
          相关资源
          最近更新 更多