I'm trying to detect when a scripts stdout is redirected, and then create another fd to accept the redirection but put fd 1 back to the terminal. I need fd 1 & fd 2 pointing at the terminal and fd 3 pointing at the file. All of them open at the same time.
Setting up fd 3 is OK. but I can't get fd 1 connected back to the terminal as though redirection never happened.
Assume the script below is called redir.
#!/bin/bash if ! [ -t 1 ]; then exec 3>&1 echo "went to 3">&3
1>&- # exec 1>/proc/self/fd/1 # exec 1>/dev/stdout # exec 1>/dev/fd/1 echo "went to 1" fi
As is produces: address@hidden ycc# ./redir >xxx; cat xxx went to 3 went to 1
I closed fd1 and yet the naked echo ends up in the file. I don't understand how that works after a close.
The 3 comments are my attempts to reconnect fd back to the console / terminal. Activating any one of them causes the output that went to fd3 to disappear, probably do to fd 1 stepping on the file anew.
How does one connect a fd to the console, NOT USING fd 1 or fd 2 targets?