我正在尝试在rails index动作中执行.sh文件。
我尝试exec"sh app/controllers/file.sh"
它在终端执行它然后停止服务器!我不知道为什么。我尝试output = system"sh app/controllers/file.sh"
它在浏览器中返回true而不是.sh文件中的内容
提前致谢 :)
你想看看Open3
类,特别是capture2
或capture3
:
require 'open3'
stdout, stderr, status = Open3.capture2("sh app/controllers/myscript.sh")
如上所示,capture3
将为您提供脚本运行的stdout
,stderr
和status
。虽然capture2
将返回stdout
和status
。 Open3
还有其他有用的功能,值得一看。
要理解为什么exec
和system
表现得如此,你可以阅读这个SO答案:Ruby, Difference between exec, system and %x() or Backticks