سؤال

How to make all stderr goes to stdout?

The case is, I'm writing a simple Ruby CGI script, when in development mode, I want to make all gems I used when giving an error, it shows on stdout (browser)

هل كانت مفيدة؟

المحلول

To redirect stderr to stdout from inside a Ruby script you can use the IO#reopen method:

# script.rb
$stderr.reopen($stdout)
fail 'error' # Messages from execeptions are usually printed on stderr

If you run the script in the shell, redirecting stderr to /dev/null, you can see that the exception message is printed on stdout:

$ ruby script.rb 2> /dev/null
foo.rb:2:in `<main>': foo (RuntimeError)

Edit: That said you probably will have to find a more appropriate solution to print error messages on your browser, for example you will need to encode HTML entities in error messages.

نصائح أخرى

Add this as part of script.

2> stands for STDERR. &1 is a reference to STDOUT.

2>&1

Example:

script.rb > log.txt 2>&1
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top