【问题标题】:How to pass shell script variables back to ruby?如何将shell脚本变量传回ruby?
【发布时间】:2016-03-10 04:09:06
【问题描述】:

我有一个执行 shell 脚本的 ruby​​ 脚本。如何将 shell 脚本数据传回 ruby​​ 脚本。

      desc "Runs all the tests"
      lane :test do 

        sh "../somescript.sh"

        print variables_inside_my_script // i want to access my script data here.

      end

我可以使用从 ruby​​ 到 shell 脚本的环境变量来执行相反的场景。

      desc "Runs all the tests"
      lane :test do 

        puts ENV["test"]

        sh "../somescript.sh" // access test using $test

      end

谢谢

【问题讨论】:

  • 不可能。子进程继承父环境的副本;他们不能影响父母的环境。如果您想将数据从 shell 脚本传递到调用进程,最简单的方法是通过其输出。

标签: ruby shell fastlane


【解决方案1】:

不清楚 variables_inside_my_script 在这里应该是什么意思,但作为一项规则,操作系统不允许将变量从子外壳“导出”到父外壳,因此 ruby​​ists 经常调用子命令带有反引号(或等效项),以便父级可以读取子shell的输出(stdout),例如

output = %x[ ls ]

根据您的实际需要,有一些可能有用的替代技术 - 参见例如

  1. Exporting an Environment Variable in Ruby

  2. http://tech.natemurray.com/2007/03/ruby-shell-commands.html

  3. http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html

【讨论】:

  • 谢谢,我试试看。
【解决方案2】:

如果 shell 脚本在您的控制之下,请让脚本以 Ruby 语法将环境定义写入 STDOUT。在 Ruby 中,你 eval 输出:

eval `scriptsettings.sh`

如果您的脚本产生其他输出,请将环境定义写入临时文件并使用load 命令读取它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多