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?
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?
Just use backquotes to execute the statement:
output = `whoami`
The result will be in the 'output' variable.
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"
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