LoginSignup
0
0

Bashで標準出力と標準エラー出力を別々のファイルに書き出す方法

Posted at
なにかコマンド 2> 標準エラー出力を書き出す先 > 標準出力を書き出す先

# ファイルに書き出しつつ画面にも表示する場合
なにかコマンド 2> >(tee 標準エラー出力を書き出す先) > >(tee 標準出力を書き出す先)

とすることで標準出力と標準エラー出力を別々の場所に出力できます。

実際に動くコマンドで書くと

bash -c 'echo "to standard out"; echo "to standard error out" >&2' 2> >(tee error_log.txt) > >(tee log.txt)
# => to standard error out
# => to standard out

cat log.txt
# => to standard out

cat error_log.txt
# => to standard error out

となります。

ちなみに

bash -c 'echo "to standard out"; echo "to standard error out" >&2' 1> >(tee log.txt) 2> >(tee error_log.txt)

とすると両方ともlog.txtに書き出されてしまうので注意が必要です。
(理由に関してはうまく言語化できていません...Bash力不足です...)

参考文献
https://aquasoftware.net/blog/?p=1663

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0