tags:

views:

117

answers:

3

I am running a command using sh and need to read the output of that command. e.g.

sh "whoami"

But sh only seems to return true rather than a string containing the output of the whoami command. Does anyone know of a solution?

+1  A: 

Just use backquotes to execute the statement:

output = `whoami`

The result will be in the 'output' variable.

kaleidomedallion
+2  A: 

There are several ways:

output = `whoami`

#or

output = %x[whoami]

# or using 'system' but in case of errors it's gonna return false

output = system "whoami"
khelll
Thank you for mentioning 'system "whoami"' -- I was looking for a way to suppress the echo-back of the command which 'sh "diff a b"' produces, and 'system "diff a b"' does the trick.
bjnord
A: 

i wasn't sure how to get those other methods to fail on error, so i went with redirection:

sh "mysql --verbose #{connection_options} < #{sql_file} > #{sql_file_output_file}" do |ok, status|
  ok or fail "mysql file failed [#{sql_file}"
end
scott