Ruby 使用rake sh时,如何检索命令的输出?

Ruby 使用rake sh时,如何检索命令的输出?,ruby,rake,Ruby,Rake,我正在使用sh运行一个命令,需要读取该命令的输出。e、 g 嘘“哇” 但是sh似乎只返回true,而不是包含whoami命令输出的字符串。有人知道解决方案吗?只需使用反引号来执行语句: output = `whoami` 结果将出现在“output”变量中。有几种方法: output = `whoami` #or output = %x[whoami] # or using 'system' but in case of errors it's gonna return false o

我正在使用sh运行一个命令,需要读取该命令的输出。e、 g

嘘“哇”


但是sh似乎只返回true,而不是包含whoami命令输出的字符串。有人知道解决方案吗?

只需使用反引号来执行语句:

output = `whoami`

结果将出现在“output”变量中。

有几种方法:

output = `whoami`

#or

output = %x[whoami]

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

output = system "whoami"

我不确定如何让其他方法在出错时失败,所以我选择了重定向:

sh "mysql --verbose #{connection_options} < #{sql_file} > #{sql_file_output_file}" do |ok, status|
  ok or fail "mysql file failed [#{sql_file}"
end
sh“mysql--verbose#{connection_options}<#{sql_file}>{sql_file_output_file}”do | ok,状态|
确定或失败“mysql文件失败[#{sql_文件}”
结束

感谢您提到“system“whoami”-我正在寻找一种方法来抑制“sh”diff a b“生成的命令的回显,而“system”diff a b”则起到了作用。