System() on windows

I want to capture the string that system("hostname") produces on windows
and store it in a variable. system() returns true or false. How can I
make it return the result (the hostname as a string) instead?

···

--
Posted via http://www.ruby-forum.com/.

Use backticks:

hostname = `hostname`.chomp

···

On Jul 15, 2006, at 10:12 PM, brad tilley wrote:

I want to capture the string that system("hostname") produces on windows
and store it in a variable. system() returns true or false. How can I
make it return the result (the hostname as a string) instead?

--
Posted via http://www.ruby-forum.com/\.

Logan Capaldo wrote:

···

On Jul 15, 2006, at 10:12 PM, brad tilley wrote:

I want to capture the string that system("hostname") produces on
windows
and store it in a variable. system() returns true or false. How can I
make it return the result (the hostname as a string) instead?

--
Posted via http://www.ruby-forum.com/\.

Use backticks:

hostname = `hostname`.chomp

If you want to catch all output including both STDOUT and STDERR you
should do following trick

output = `someprogram 2>&1`

So using OS pipes you redirect STDERR to STDOUT and then run application
and get STDOUT using `` function.

--
Posted via http://www.ruby-forum.com/\.

If you want to catch all output including both STDOUT and STDERR you
should do following trick

output = `someprogram 2>&1`

So using OS pipes you redirect STDERR to STDOUT and then run application
and get STDOUT using `` function.

Except that the OP said he is on Windows, and unless he is using cygwin
to get bash/sh style redirection, that won't work. Starndard Error
isn't captured by any of the ruby IO/Exec processes that I can find.

Steve Martin wrote:

If you want to catch all output including both STDOUT and STDERR you
should do following trick

output = `someprogram 2>&1`

So using OS pipes you redirect STDERR to STDOUT and then run application
and get STDOUT using `` function.

Except that the OP said he is on Windows, and unless he is using cygwin
to get bash/sh style redirection, that won't work. Starndard Error
isn't captured by any of the ruby IO/Exec processes that I can find.

Did you really try my recipe on Windows???

File.open('a.rb','w'){|f|
  f.write <<-END
    STDOUT.puts 'standard output stream'
    STDERR.puts 'error output stream'
  END
}

out = `ruby a.rb 2>&1`

puts "HERE IT IS OUTPUT:"
puts out

=====OUTPUT=====
HERE IT IS OUTPUT:
standard output stream
error output stream

···

--
Posted via http://www.ruby-forum.com/\.

I stand corrected. I did try it, but got it twisted up. Yet more
evidence that Windows has moved closer to a Unix syntax for the command
shell. Handy to know if I ever end up doing development on Windows.

···

Steve Martin wrote:
> Except that the OP said he is on Windows, and unless he is using cygwin
> to get bash/sh style redirection, that won't work. Starndard Error
> isn't captured by any of the ruby IO/Exec processes that I can find.

Did you really try my recipe on Windows?

File.open('a.rb','w'){|f|
  f.write <<-END
    STDOUT.puts 'standard output stream'
    STDERR.puts 'error output stream'
  END
}

out = `ruby a.rb 2>&1`

puts "HERE IT IS OUTPUT:"
puts out

=====OUTPUT=====
HERE IT IS OUTPUT:
standard output stream
error output stream