tags:

views:

29

answers:

3

Hi.

I want to be able to toggle between redirects. ie:

Redirect #1 would be: Don't redirect to stdout, Redirect the output to a file.

Redirect #2 would be: Don't redirect to file... rather redirect to stdout

Is it possible to do this at runtime?

A: 

You can redirect from inside a bash script (so it is not the caller to decide) with

exec >file

or

exec >$(tty)

enzotib
A: 

As a quasi-medieval alternative, you could install Konsole or some other terminal that let's you save the output to a file. You can save the output at any point while an application is running (runtime) on your terminal.

On Konsole you would click on: Scrollback > Save Output

You didn't specifically said you were looking for a programming solution, so I believe the idea is valid.

karlphillip
+6  A: 

Here it goes for bash (for C it would be pretty the same):

#!/bin/bash

echo begin experiment

exec 3>&1-

exec > to_file

echo this goes to file
echo another line to file

exec 1>&3-

echo this goes again to stdout
dma_k
One more redirect example (http://www.ashep.org/2010/perenapravleniya-v-bash-pri-pomoshhi-exec) but in Russian.
dma_k