0

I have a bash script where I'm using the following to direct the stdout/stderr of every command to a file:

today=`/bin/date '+%Y_%m_%d__%H_%M_%S'`
exec 2> "/home/pi/stream_logs/$today.$RANDOM.txt"
exec 1>&2

[command 1
command 2
command 3
command 4
.... (many more commands)]

I'd like to redirect the stdout/stderr of command 2 to a DIFFERENT file, while keeping the stdout/stderr of every other command going to /home/pi/stream_logs/$today.$RANDOM.txt.

How can I override & redirect JUST the output of one command in this script to a separate file?

2
  • The commands are exactly sequential? Can command2 happen before command1? Commented Mar 31, 2017 at 10:18
  • Command2 has to happen in the middle of a clump of commands Commented Mar 31, 2017 at 10:40

1 Answer 1

1

You can simply do this :

...
command 1
command 2 >path_of_secondary_output_file
command 3
...

Each command can be redirected individually. When not redirected, the default is to connect to the stdout and stderr of the calling context (which is why exec has an effect on your commands), but that does not prevent redirecting individual commands.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.