Restoring stdin and stderr (in a logging function)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

Restoring stdin and stderr (in a logging function)

#1 Post by s243a »

In a log function, I have I restored stdin and stderr as follows:

Code: Select all

exec 6>&1           # Link file descriptor #6 with stdout.
exec &> >(tee -a "$LOGFILE")
....
exec 1>&6  
exec 6>&-      # Restore stdout and close file descriptor #6.
exec &2> /dev/stderr   
https://gitlab.com/s243a/psandbox/-/blo ... box.sh#L90

I'm looking for coding suggestions here. One thing that I think might be a good idea is if for some reason "6" ends up being a bad file descriptor I should do a fallback like:

Code: Select all

exec 1> /dev/stdin
or
exec 1> /dev/console
I also saw the following suggestion:
0

If the program runs on a Linux environment, you can freopen ("/dev/stdout", "a", stdout).

But if you know that stdout was the terminal, freopen ("/dev/tty", "a", stdout) or the equivalent for other OSs—even Windows.
https://stackoverflow.com/questions/110 ... o-terminal

also what is the difference between:

Code: Select all

exec 1>&6  
and

Code: Select all

exec &1>&6  
I think that they will both work.
Find me on [url=https://www.minds.com/ns_tidder]minds[/url] and on [url=https://www.pearltrees.com/s243a/puppy-linux/id12399810]pearltrees[/url].

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#2 Post by jamesbond »

Re: your last question, the answer is: they are the same for bash.
For other shells, only the first form will work. So beware.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

Post Reply