patrick-may / monmouth.com (Patrick May) writes:

> "Richard A. Ryan" <ryan / fsl.noaa.gov> wrote in message news:<3D5D7E39.7050601 / fsl.noaa.gov>...
> > How would I do the equivalent to the following
> > Korn/Bourne code in ruby?
> 
> perhaps like this?
> 
> old_stderr = $stderr
> begin
>     File.open( "logfile.text", "a" ) { |log|
>         $stderr = logfile
>         #... do stuff
>     }
> ensure
>     $stderr = old_stderr
> end
> 
> ~ Patrick


Thank you for the suggestion, but perhaps I should have been more
specific about what I really want to do.  The following is closer to
what I'm interested in.  It's a very common lick to play in Korn, perl
and C and if I have to go through a symphony to do the same thing in
ruby then, to paraphrase the great American poet Chuck Berry, I'll
lose the beauty of the melody.

########################################################################
# For Korn, bash, zsh, POSIX shell and possibly maybe even Bourne
# Save defaults for STDOUT AND STDERR
exec 3>&1
exec 4>&2
# Send STDOUT and STDERR to a log file
exec 1> /tmp/logfile.out 2>&1

echo hello world
ls -l nonexistent_file
ls -l /etc/passwd
call_nonexistent_command

# Restore STDOUT and STDERR
exec 1>&3
exec 2>&4
# Close extra file descriptors
exec 3>&-
exec 4>&-

echo hello again world
ls -l nonexistent_file_again
ls -l /etc/group
call_nonexistent_command_again
########################################################################