代码之家  ›  专栏  ›  技术社区  ›  João Portela

将stdout和stderr重定向到带有前缀的单个文件

  •  8
  • João Portela  · 技术社区  · 16 年前

    我正在编写bash脚本,需要重定向 stdout stderr 我运行到单个文件中的命令的输出,在每一行的前缀中 标准错误 标准输出 相应地。

    有简单的方法吗?

    2 回复  |  直到 16 年前
        1
  •  11
  •   ephemient    16 年前

    annotate-output ,从 Debian devscripts ,执行此操作。

    其手册页中的示例:

    $ annotate-output make
    21:41:21 I: Started make
    21:41:21 O: gcc -Wall program.c
    21:43:18 E: program.c: Couldn't compile, and took me ages to find out
    21:43:19 E: collect2: ld returned 1 exit status
    21:43:19 E: make: *** [all] Error 1
    21:43:19 I: Finished with exitcode 2
    
        2
  •  10
  •   mouviciel    16 年前

    试试这个:

    (myCommand | sed s/^/stdout:/ >> myLogfile) 2>&1 | sed s/^/stderr:/ >> myLogFile
    

    第一个管道插入 stdout: 标准输出的前缀 myCommand 并附加到 myLogFile .

    圆括号用于对所有这些内容进行单个命令。它们告诉我们,进一步的重定向适用于括号内的内容,而不是 sed 只有。

    然后将标准错误重定向到标准输出 2>&1 (请记住,原始标准输出已重定向到 我的日志文件 )第二个管道插入件A stderr: 前缀并附加到 我的日志文件 .

    推荐文章