Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve the output of my command when using rake sh?

Tags:

ruby

rake

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?

like image 592
ChrisInCambo Avatar asked Sep 17 '09 23:09

ChrisInCambo


3 Answers

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"
like image 148
khelll Avatar answered Nov 07 '22 20:11

khelll


Just use backquotes to execute the statement:

output = `whoami`

The result will be in the 'output' variable.

like image 31
prismofeverything Avatar answered Nov 07 '22 20:11

prismofeverything


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
like image 1
scott Avatar answered Nov 07 '22 18:11

scott