【问题标题】:How to execute .sh file in ruby on rails如何在 ruby​​ on rails 中执行 .sh 文件
【发布时间】:2019-09-10 04:06:54
【问题描述】:

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

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

提前致谢:)

【问题讨论】:

    标签: ruby-on-rails ruby sh


    【解决方案1】:

    您需要查看Open3 类,特别是capture2capture3

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

    正如您在上面看到的,capture3 将为您提供脚本运行的 stdoutstderrstatus。而capture2 只会返回stdoutstatusOpen3中还有其他有用的功能值得关注。

    要了解 execsystem 的行为方式为何,您可以阅读此 SO 答案:Ruby, Difference between exec, system and %x() or Backticks

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 2016-02-27
      • 2016-12-05
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      相关资源
      最近更新 更多