Can you temporarily turn off STDERR?

Inside a ruby script I want to:

1) redirect STDERR to /dev/null
2) Issue a system call (e.g. system(tar xf tarfile file) )
3) revert to normal STDERR output

I have a script that issues 3-4 system calls that are returning the
following message to STDERR:

warning: Insecure world writable dir /opt, mode 040777

I'd like to suppress these messages (Let's assume that I can't change
the permissions on /opt).

An example of one of the lines that is throwing the error is:

x = `tar tf mytar.tar` # envoke the unix tar command
print x

I can suppress the messages by:

$stderr.reopen('/dev/null', 'w') # send STDERR to /dev/null
STDERR.puts "can you see me?" # message is suppressed

But I haven't been able to figure out how to revert STDERR back to
before the the reopen. I only want to suppress this particular
message, but not others that may prove to be useful such as error
messages from the ruby interpreter.

I've tried variations of:

stderr = $stderr # save current STDERR IO instance
$stderr.reopen('/dev/null', 'w') # send STDERR to /dev/null
STDERR.puts "can you see me?" # message is suppressed
$stderr.reopen(stderr) # revert to default behavior (doesn't
work)
STDERR.puts "what about me? # I want to see this but I don't

Any suggestions?

Inside a ruby script I want to:

1) redirect STDERR to /dev/null
2) Issue a system call (e.g. system(tar xf tarfile file) )
3) revert to normal STDERR output

I have a script that issues 3-4 system calls that are returning the
following message to STDERR:

warning: Insecure world writable dir /opt, mode 040777

I'd like to suppress these messages (Let's assume that I can't change
the permissions on /opt).

An example of one of the lines that is throwing the error is:

x = `tar tf mytar.tar` # envoke the unix tar command
print x

the warning is from ruby.

   $VERBOSE = nil

will shut it up

if you want it shut up only sometimes do

   def quiet
     v = $VERBOSE
     yield
   ensure
     $VERBOSE = v
   end

then

   x = quietly{ `tar tf mytar.tar` }
   print x

Any suggestions?

if you really want fine control over stdin/stdout/stderr of executed commands
check out my session and open4 libs

   http://codeforpeople.com/lib/ruby/session/session-2.4.0/README
   http://codeforpeople.com/lib/ruby/open4/open4-0.5.1/README

in particular the Open4::spawn command. both are available as

   gem install session
   gem install open4

regards.

-a

···

On Sat, 26 Aug 2006, bradjpeek wrote:
--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

Tar is being run in a shell, so you could run:
system("tar xf mytar.tar 2>/dev/null")

You could also use the open3 library (from the standard library)
documented on p708 of Pickaxe or at
http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html

--Ken

···

On Fri, 25 Aug 2006 12:27:35 -0700, bradjpeek wrote:

Inside a ruby script I want to:

1) redirect STDERR to /dev/null
2) Issue a system call (e.g. system(tar xf tarfile file) )
3) revert to normal STDERR output

I have a script that issues 3-4 system calls that are returning the
following message to STDERR:

warning: Insecure world writable dir /opt, mode 040777

I'd like to suppress these messages (Let's assume that I can't change
the permissions on /opt).

An example of one of the lines that is throwing the error is:

x = `tar tf mytar.tar` # envoke the unix tar command
print x

I can suppress the messages by:

$stderr.reopen('/dev/null', 'w') # send STDERR to /dev/null
STDERR.puts "can you see me?" # message is suppressed

But I haven't been able to figure out how to revert STDERR back to
before the the reopen. I only want to suppress this particular
message, but not others that may prove to be useful such as error
messages from the ruby interpreter.

I've tried variations of:

stderr = $stderr # save current STDERR IO instance
$stderr.reopen('/dev/null', 'w') # send STDERR to /dev/null
STDERR.puts "can you see me?" # message is suppressed
$stderr.reopen(stderr) # revert to default behavior (doesn't
work)
STDERR.puts "what about me? # I want to see this but I don't

Any suggestions?

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Modify that slightly:

$stderr_backup = $stderr.dup
$stderr.reopen("/dev/null", "w")
STDERR.puts "should not see this"
$stderr = $stderr_backup.dup
STDERR = $stderr # can safely ignore the warning
STDERR.puts "foo" # will print foo to the screen

I make no guarantees that this is the best way to do it, infact I can safely say it's probably not.

···

On 06-08-25, at 15:30, bradjpeek wrote:

Inside a ruby script I want to:

1) redirect STDERR to /dev/null
2) Issue a system call (e.g. system(tar xf tarfile file) )
3) revert to normal STDERR output

I have a script that issues 3-4 system calls that are returning the
following message to STDERR:

warning: Insecure world writable dir /opt, mode 040777

I'd like to suppress these messages (Let's assume that I can't change
the permissions on /opt).

An example of one of the lines that is throwing the error is:

x = `tar tf mytar.tar` # envoke the unix tar command
print x

I can suppress the messages by:

$stderr.reopen('/dev/null', 'w') # send STDERR to /dev/null
STDERR.puts "can you see me?" # message is suppressed

But I haven't been able to figure out how to revert STDERR back to
before the the reopen. I only want to suppress this particular
message, but not others that may prove to be useful such as error
messages from the ruby interpreter.

I've tried variations of:

stderr = $stderr # save current STDERR IO instance
$stderr.reopen('/dev/null', 'w') # send STDERR to /dev/null
STDERR.puts "can you see me?" # message is suppressed
$stderr.reopen(stderr) # revert to default behavior (doesn't
work)
STDERR.puts "what about me? # I want to see this but I don't

Any suggestions?

--
Jeremy Tregunna
jtregunna@blurgle.ca

You, sir, are a scholar and a gentleman. Worked like a charm.

it's ruby that prints the message, not the command

   jib:~/shared > ruby -e' system "true" '
   -e:1: warning: Insecure world writable dir /dmsp/moby-1-1/ahoward/shared/.,
   mode 040777

fyi.

cheers.

-a

···

On Sat, 26 Aug 2006, Ken Bloom wrote:

Tar is being run in a shell, so you could run:
system("tar xf mytar.tar 2>/dev/null")

You could also use the open3 library (from the standard library)
documented on p708 of Pickaxe or at
http://www.ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama