【问题标题】:Print STDOUT/STDERR and write them to a file in Bash?打印 STDOUT/STDERR 并将它们写入 Bash 中的文件?
【发布时间】:2023-03-23 02:51:01
【问题描述】:

有没有办法让 Bash 将 STDOUT/STDERR 重定向到文件但仍将它们打印到终端?

【问题讨论】:

    标签: linux bash stdout stderr io-redirection


    【解决方案1】:

    这会将 STDOUT 和 STDERR 重定向到同一个文件:

    some_command 2>&1 | tee file.log
    

    示例

    $ touch foo; ls foo asfdsafsadf 2>&1 | tee file.log
    ls: asfdsafsadf: No such file or directory
    foo
    $ cat file.log
    ls: asfdsafsadf: No such file or directory
    foo
    

    【讨论】:

    • 如果不想每次都覆盖file.log,请使用tee -a file.log
    【解决方案2】:

    使用tee 命令。

    $ echo "hi" | tee output.txt
    hi
    [unix]$ ls
    output.txt
    [unix]$ cat output.txt 
    hi
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多