Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

bash - Redirecting stdout/stderr to multiple files

I was wondering how to redirect stderr to multiple outputs. I tried it with this script, but I couldn't get it to work quite right. The first file should have both stdout and stderr, and the 2nd should just have errors.

perl script.pl &> errorTestnormal.out &2> errorTest.out

Is there a better way to do this? Any help would be much appreciated. Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

perl script.pl 2>&1 >errorTestnormal.out | tee -a errorTestnormal.out > errorTest.out

Will do what you want.

This is a bit messy, lets go through it step by step.

  • We say what used to go to STDERR will now go STDOUT
  • We say what used to go to STDOUT will now go to errorTestnormal.out.

So now, STDOUT gets printed to a file, and STDERR gets printed to STDOUT. We want put STDERR into 2 different files, which we can do with tee. tee appends the text it's given to a file, and also echoes to STDOUT.

  • We use tee to append to errorTestnormal.out, so it now contains all the STDOUT and STDERR output of script.pl.
  • Then, we write STDOUT of tee (which contains STDERR from script.pl) into errorTest.out

After this, errorTestnormal.out has all the STDOUT output, and then all the STDERR output. errotTest.out contains only the STDERR output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...