簡體   English   中英

如何將stderr和stdout重定向到多個輸出文件?

[英]How to redirect stderr and stdout to more than one output file?

我想知道如何為以下命令編寫3個命令:

  • 將輸出從stdout重定向到文件output.txt
  • 將輸出從stderr重定向到文件error.txt
  • 將輸出從stdout和stderr重定向到文件all.txt

那是我到目前為止的代碼,但是沒有輸出任何內容:

 #!/bin/bash
    echo This goes to stdout
    echo And this is and error going to stderr 1>&2
    exec 1>output.txt
    exec 2>error.txt
    exec >all.txt 1>&2

您可以結合使用tee命令和進程替換來完成

#!/bin/bash

exec 3> all.txt # fd3 goes to all.txt
exec 1> >(tee output.txt >&3) # fd1(stdout) goes to both output.txt and fd3
exec 2> >(tee error.txt >&3) # fd2(stderr) goes to both error.txt and fd3

echo Go To Stdout # goes to fd1, and fd1 goes to both output.txt and fd3 (which goes to all.txt)
echo Go To Stderr >&2 # goes to fd2, and fd2 goes to both error.txt and fd3 (which goes to all.txt)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM