user402642
user402642

Reputation:

Linux Redirections; Possible to Toggle Between Redirects?

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?

Upvotes: 1

Views: 81

Answers (3)

dma_k
dma_k

Reputation: 10639

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

Upvotes: 5

karlphillip
karlphillip

Reputation: 93468

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.

Upvotes: 0

enzotib
enzotib

Reputation: 1644

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

exec >file

or

exec >$(tty)

Upvotes: 0

Related Questions