bash: how to restore output to the term after exec>FILE?

bash: how to restore output to the term after exec>FILE?

Post by nospam5 » Sun, 25 Jan 2004 16:17:34



Hi.

In my .bashrc script I say

    exec > .bash.startup.log

later I want output back to the terminal , how can I get this ? Maybe kind of

    exec &> /dev/tty

? Will this be reliable in generic bash scripts ?

   Thanks :-)

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Alan Conno » Sun, 25 Jan 2004 18:28:27



> Hi.

> In my .bashrc script I say

>     exec > .bash.startup.log

> later I want output back to the terminal , how can I get this ? Maybe kind of

>     exec &> /dev/tty

> ? Will this be reliable in generic bash scripts ?

>    Thanks :-)

I don't think you need that first exec.

You are wanting to empty that log file, I assume, and this will do it:

:> .bash.startup.log

What happens when you use exec, is that you replace the shell with that
program, so I guess you'd have to kill it by finding its PID and
doing a kill -9 PID.

kill -9 $$

Might do it.

(You might want to put that command in a file called .bash_logout to
run as the shell exited rather than started up.)

AC

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Chris F.A. Johnso » Sun, 25 Jan 2004 18:32:39



> Hi.

> In my .bashrc script I say

>     exec > .bash.startup.log

> later I want output back to the terminal , how can I get this ? Maybe kind of

>     exec &> /dev/tty

    Save stdout, then redirect:

exec 3>&1 >.bash.startup.log

    Restore stdout:

exec >&3 3>-

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by j.. » Mon, 26 Jan 2004 07:50:13




> > Hi.

> > In my .bashrc script I say

> >     exec > .bash.startup.log

> > later I want output back to the terminal , how can I get this ?
> > Maybe kind of

> >     exec &> /dev/tty

> > ? Will this be reliable in generic bash scripts ?

> >    Thanks :-)

> I don't think you need that first exec.

He does if he want's to redirect stdout without having to do it every
time he issues output.

Quote:> You are wanting to empty that log file, I assume, and this will do
> it:

What he said was he wanted stdout to go to the log file, and then
later to restore stdout to the original descriptor. Chris gave that
solution.

Quote:> :> .bash.startup.log

> What happens when you use exec, is that you replace the shell with
> that program, so I guess you'd have to kill it by finding its PID
> and doing a kill -9 PID.

What happens when you do exec this way is to open, close or redirect
file descriptors. Check the man page.

Quote:> kill -9 $$

> Might do it.

exit would be all he needs to do that.

Quote:> (You might want to put that command in a file called .bash_logout to
> run as the shell exited rather than started up.)

How would he be able to get output from his startup script by doing
that?

Joe
--
I think people should be able to make up their own minds for me
  - Monty Python

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Michael Tos » Tue, 27 Jan 2004 01:55:50




> > Hi.

> > In my .bashrc script I say

> >     exec > .bash.startup.log

> > later I want output back to the terminal , how can I get this ? Maybe kind of

> >     exec &> /dev/tty

>     Save stdout, then redirect:

> exec 3>&1 >.bash.startup.log

>     Restore stdout:

> exec >&3 3>-

typo, should be
exec >&3 3>&-  #restore stdout, then close descriptor 3
or just
exec >&3

And please not: exec >&3 3>- && rm ./- #;-)

--
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Chris Matter » Wed, 28 Jan 2004 02:07:13



> Hi.

> In my .bashrc script I say

>     exec > .bash.startup.log

> later I want output back to the terminal , how can I get this ? Maybe kind of

>     exec &> /dev/tty

Actually, exec > /dev/tty will put stdout to terminal.  But...

Quote:

> ? Will this be reliable in generic bash scripts ?

No.  What if your stdout wasn't /dev/tty to begin with?  The *right*
way to do this is to save your stdout first in a spare file descriptor.
Since fd 0 is stdin, fd 1 is stdout, and fd 2 is stderr, your first
available fd is likely to be 3.

exec 3> &1 # save stdout in fd 3
exec > .bash.startup.log

# and, when you're finished
exec > &3 # get back original stdout
exec 3> &- # and close fd 3

             Chris Mattern

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Stephane CHAZELA » Wed, 28 Jan 2004 02:35:42


2004-01-26, 12:07(-05), Chris Mattern:
[...]

Quote:> exec 3> &1 # save stdout in fd 3

Which shell are you using?

$ zsh -c 'exec 3> &1'
zsh: parse error near `&'
$ pdksh -c 'exec 3> &1'
pdksh: syntax error: `&' unexpected
$ ksh93 -c 'exec 3> &1'
ksh93: syntax error at line 1: `&' unexpected
$ bash -c 'exec 3> &1'
bash: -c: line 1: syntax error near unexpected token `&'
bash: -c: line 1: `exec 3> &1'
$ dash -c 'exec 3> &1'
dash: Syntax error: "&" unexpected

exec 3>& 1
exec 3>&1

are OK.

--
Stphane                      ["Stephane.Chazelas" at "free.fr"]

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by nospam5 » Wed, 28 Jan 2004 04:37:36


Thank you very much to you all for your suggestions;
I will try to use them and let you know.

By the way: believe it or not, after 2 or three
times I read quite tutorial stuff on this issue
of bash , I still use file descriptors in bash
(say, kind of

    exec >&3 3>-

) almost as if it were black magic, i.e. I
didn't manage to get an even elementary understanding
on the subject (maybe the reason is that I never programmed in C?)

Do you know friendly literature helping getting
some little intuition on that ?

      nospam55 :-)

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Chris Matter » Wed, 28 Jan 2004 07:55:20



> 2004-01-26, 12:07(-05), Chris Mattern:
> [...]

>>exec 3> &1 # save stdout in fd 3

> Which shell are you using?

> $ zsh -c 'exec 3> &1'
> zsh: parse error near `&'
> $ pdksh -c 'exec 3> &1'
> pdksh: syntax error: `&' unexpected
> $ ksh93 -c 'exec 3> &1'
> ksh93: syntax error at line 1: `&' unexpected
> $ bash -c 'exec 3> &1'
> bash: -c: line 1: syntax error near unexpected token `&'
> bash: -c: line 1: `exec 3> &1'
> $ dash -c 'exec 3> &1'
> dash: Syntax error: "&" unexpected

> exec 3>& 1
> exec 3>&1

You got me, the spaces don't belong there.

              Chris Mattern

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by nospam5 » Thu, 29 Jan 2004 02:28:20


Thank you! the

        exec 3>&1 >a5.log # save way, redirect
      same
        exec 3>&1        # save curr stdout in fd 3
        exec >a5.log     # curr stdout onto file  a5.log

      <code>

    now restore
        exec >&3    # restore original stdout setting
          # exec > /dev/tty # an alternative, if you are sure you want the term iof the original
stdout setting
        exec 3>&-   # close file descriptor 3
    same
        exec >&3 3>&-  

works perfectly ! :-)

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by Michael Tos » Thu, 29 Jan 2004 03:53:13



> Thank you very much to you all for your suggestions;
> I will try to use them and let you know.

> By the way: believe it or not, after 2 or three
> times I read quite tutorial stuff on this issue
> of bash , I still use file descriptors in bash
> (say, kind of

>     exec >&3 3>-

> ) almost as if it were black magic, i.e. I
> didn't manage to get an even elementary understanding
> on the subject (maybe the reason is that I never programmed in C?)

> Do you know friendly literature helping getting
> some little intuition on that ?

>       nospam55 :-)

Ok, an intro:

<  or  0<  is stdin (standard input stream)

Quote:>  or  1>  is stdout (standard output stream)

2>         is stderr (standard error stream)

(the numbers are Unix-specific, so /bin/sh is probably not directly
portable to other OS. C therefore always sais STDIN, never 0)

command | mail ...
redirects stdout (#1) to mail and leave stderr unaffected.

command 2>/dev/null | mail ...
redirects stderr (#2) to /dev/null and stdout (#1) to mail.

command 2>&1 | mail
redirects stderr to where stdout goes, and then stdout to mail.
Effectively both stdout and stderr go to mail.

exec 2>&1
permanently redirects the shell's stderr where stdout goes.
A subsequent
command | mail
still redirects only command's stdout to mail, and echoes stderr,
but an external filter cannot separate between stdout and stderr.
This redirection cannot be undone, because there is no pointer
left to where stderr (#2) had originally pointed to.

Most Unixes offer more file descriptors 3,4,5,... that can be
used to save descriptors, or to duplicate streams,...

exec 3>&1
redirects #3 to where stdout goes (#3 points now to where #1
points to).

exec >.bash.startup.log
redirects stdout (#1) to the file.

Most /bin/sh allow to put multiple exec in one line:
exec 3>&1 >.bash.startup.log

exec >&3  (or exec 1>&3) restores stdout from #3.

&- is a closed file handle.
exec 3>&-
effectively closes #3

--
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313

 
 
 

bash: how to restore output to the term after exec>FILE?

Post by nospam5 » Sat, 31 Jan 2004 02:22:20


Thank you Michael,

I will study thoroughly. :-)

 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. Sparc Console on Modem?

3. want sh script to exec itself with >file 2>&1

4. NIS+-Server

5. Poor (?) DPT Raid-5 performance

6. DUMP/RESTORE File -> TAR File Converter?

7. Samba and Windows plain text passwords

8. Diverting Output Within File With Exec

9. What's the difference between in Exec and Exec exec ...

10. Need 'exec.h' file or 'struct exec' definition

11. using lpd->apsfilter->ghostscript->hpijs = no output

12. vi: file.clear==>file.encrypt==>file.clear ?