user2989813
user2989813

Reputation: 283

Redirect output of just one command to different file than rest of script

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?

Upvotes: 0

Views: 104

Answers (1)

Fred
Fred

Reputation: 7005

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.

Upvotes: 1

Related Questions