我正在使用sh运行一个命令,并且需要读取该命令的输出。例如:
嘘“哇塞米”
但是sh似乎只返回true,而不是包含whoami命令输出的字符串。有没有人知道解决方案?
发布于 2009-09-18 00:32:09
有几种方法:
output = `whoami`
#or
output = %x[whoami]
# or using 'system' but in case of errors it's gonna return false
output = system "whoami"发布于 2009-09-17 23:50:22
只需使用反引号来执行语句:
output = `whoami`结果将在'output‘变量中。
发布于 2010-08-13 02:36:49
我不确定如何让其他方法在出错时失败,所以我选择了重定向:
sh "mysql --verbose #{connection_options} < #{sql_file} > #{sql_file_output_file}" do |ok, status|
ok or fail "mysql file failed [#{sql_file}"
endhttps://stackoverflow.com/questions/1441894
复制相似问题