Forum: Ruby Ruby Noob: Using stderr, stdout, etc

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-03-01 23:40
(Received via mailing list)
I'm a QA tester currently using ruby/watir for testing web sites/
applications.  I've done fairly well so far for our small needs -
automating IE browsing/tasks, assertion results logging (custom
logger), and e-mail notifications for assertion errors.

I would like to log the console output from the ruby program (shown in
either DOS or SciTE) to a file just to have, but I'm not sure if this
is what stderr/stdout is talking about.  It seems to be such a well-
known concept that nobody really talks about it in detail on the web.
I know what they are by definition, but beyond that I'm having trouble
finding anything.

Any help/information you can provide is greatly appreciated.

Thanks --
Brian C. (Guest)
on 2007-03-02 00:39
(Received via mailing list)
On Fri, Mar 02, 2007 at 06:40:10AM +0900, removed_email_address@domain.invalid 
wrote:
> I would like to log the console output from the ruby program (shown in
> either DOS or SciTE) to a file just to have, but I'm not sure if this
> is what stderr/stdout is talking about.  It seems to be such a well-
> known concept that nobody really talks about it in detail on the web.
> I know what they are by definition, but beyond that I'm having trouble
> finding anything.

What platform are you running under - e.g. Windows or Unix?

If it's Unix, then stdin and stdout are well-defined and
well-documented. If
it's Windows, then I'm not sure that they are :-)
(Guest)
on 2007-03-02 03:16
(Received via mailing list)
On Mar 1, 5:38 pm, Brian C. <removed_email_address@domain.invalid> wrote:
> If it's Unix, then stdin and stdout are well-defined and well-documented. If
> it's Windows, then I'm not sure that they are :-)

Windows :D
Tim B. (Guest)
on 2007-03-02 03:18
(Received via mailing list)
>  It seems to be such a well-
> known concept that nobody really talks about it in detail on the web.
> I know what they are by definition, but beyond that I'm having trouble
> finding anything.

Hm. Under Unix, processes automatically have three streams/files
attached to them:

"At program startup, three streams are predefined and need not be
opened explicitly: standard input (for reading conventional input),
standard output (for writing conventional output) and standard error
(for writing diagnostic output)."
(http://www.opengroup.org/onlinepubs/007908799/xsh/...)

By default, stdin is the input coming from the keyboard and stderr and
stdout are output to the terminal the process is running in. This can
be manipulated, though. For example if you want the output of a script
`myprog.rb` to be written into a file named `output` rather than on
the terminal, you redirect the output like this:

$ myprog.rb > output

Under Unix (you'll need to experiment with windows) this only
redirects stdin, which is useful, because you still get to see error
messages and they won't get mixed in with the results of the program.

stdout is also where everything you write with `puts` and `printf` ends
up.

Google for something like "unix beginner's tutorial" and search around
in there for terms like `pipe` and `redirection`
   -tim
(Guest)
on 2007-03-02 17:20
(Received via mailing list)
Thanks for the explanation Tim, that's exactly what I needed.  I
didn't realize they would act differently under windows/unix - I think
I was stuck on imagining them as code-specific items because I wasn't
used to hearing about them in unix.

Strangely enough, I'm ok as far as unix and pipe/redirection, and I've
been logging output of standard unix stuff to files for a couple of
years - just never knew specifically what the output was called.  Go
figure.
Jenda K. (Guest)
on 2007-03-02 21:00
removed_email_address@domain.invalid wrote:
> Thanks for the explanation Tim, that's exactly what I needed.  I
> didn't realize they would act differently under windows/unix - I think
> I was stuck on imagining them as code-specific items because I wasn't
> used to hearing about them in unix.
>
> Strangely enough, I'm ok as far as unix and pipe/redirection, and I've
> been logging output of standard unix stuff to files for a couple of
> years - just never knew specifically what the output was called.  Go
> figure.

The thing to watch out for under Windows is that (at least on some
versions)

   script.rb > results.txt

doesn't work and you have to write

   ruby script.rb > results.txt

This is caused by the fact that the cmd.exe has to use a different WIN32
API call to execute the script using the file extension mapping than
what it would use for .exe and thatthe former doesn't preserve the
STDxxx redirection.
Rick D. (Guest)
on 2007-03-02 23:39
(Received via mailing list)
On 3/2/07, Jenda K. <removed_email_address@domain.invalid> wrote:
>
> API call to execute the script using the file extension mapping than
> what it would use for .exe and thatthe former doesn't preserve the
> STDxxx redirection.

Also, although I haven't really kept up with windows, it used to be
able to redirect stdout, but not stderr.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
Jan S. (Guest)
on 2007-03-03 00:29
(Received via mailing list)
On 3/2/07, Rick DeNatale <removed_email_address@domain.invalid> wrote:
> > > figure.
> > This is caused by the fact that the cmd.exe has to use a different WIN32
> > API call to execute the script using the file extension mapping than
> > what it would use for .exe and thatthe former doesn't preserve the
> > STDxxx redirection.
>
> Also, although I haven't really kept up with windows, it used to be
> able to redirect stdout, but not stderr.

I was surprised when I learnt that since win2k it's possible to
redirect stderr and even redirect stderr to stdout or vice versa,
using

progname 2> stderr.txt and progname 2&>1

You know, after all those years, maybe we'll be able to call cmd.exe a
shell :D

Other useful bits are:

^ is escape char - for escaping |, > etc.
|| and && work in windows as well


So... reading help might be useful and surprising sometimes...
This topic is locked and can not be replied to.