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?
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"
Just use backquotes to execute the statement:
output = `whoami`
The result will be in the 'output' variable.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With