2009-09-17 72 views

回答

5

只需使用反引號執行語句:

output = `whoami` 

結果將在「輸出」變量。

14

有幾種方法:

output = `whoami` 

#or 

output = %x[whoami] 

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

output = system "whoami" 
+0

謝謝你提的「系統「whoami」 - 我正在尋找一種方法來抑制'sh'diff'ab''產生的命令的回聲,'system'diff ab''這樣做。 – bjnord 2010-10-15 20:03:13

1

我不知道如何讓這些其他方法失敗的錯誤,所以我去重定向:

sh "mysql --verbose #{connection_options} < #{sql_file} > #{sql_file_output_file}" do |ok, status| 
    ok or fail "mysql file failed [#{sql_file}" 
end 
相關問題