help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] redirection - stdout


From: John Kearney
Subject: Re: [Help-bash] redirection - stdout
Date: Mon, 28 May 2012 08:40:39 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1

#!/bin/bash
if ! [ -t 1 ]; then
   exec 3>&1
   exec 1>$(tty)
fi

doing this
1>&-
will close &3 as well.


note doing this
exec 1>/dev/fd/1
is the same as doing this
exec 1>&1


Am 28.05.2012 04:32, schrieb Bill Gradwohl:
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?

Thank You

--
Bill Gradwohl



--
View
          John Kearney's profile on LinkedIn John
          Kearney

reply via email to

[Prev in Thread] Current Thread [Next in Thread]