如何在rails上的ruby中执行.sh文件

问题描述 投票:4回答:1

我正在尝试在rails index动作中执行.sh文件。

我尝试exec"sh app/controllers/file.sh"它在终端执行它然后停止服务器!我不知道为什么。我尝试output = system"sh app/controllers/file.sh"它在浏览器中返回true而不是.sh文件中的内容

提前致谢 :)

ruby-on-rails ruby sh
1个回答
3
投票

你想看看Open3类,特别是capture2capture3

require 'open3'
stdout, stderr, status = Open3.capture2("sh app/controllers/myscript.sh")

如上所示,capture3将为您提供脚本运行的stdoutstderrstatus。虽然capture2将返回stdoutstatusOpen3还有其他有用的功能,值得一看。

要理解为什么execsystem表现得如此,你可以阅读这个SO答案:Ruby, Difference between exec, system and %x() or Backticks

© www.soinside.com 2019 - 2024. All rights reserved.