如何将shell脚本变量传递回ruby?

如何将shell脚本变量传递回ruby?,ruby,shell,fastlane,Ruby,Shell,Fastlane,我有一个执行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脚本的环境变量执行相反的

我有一个执行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“导出”到父shell,因此rubyists经常使用backtick(或等效项)调用子命令,以便父shell可以读取子shell的输出(stdout),例如

output = %x[ ls ]
根据您的实际需要,有一些替代技术可能很有用——请参见


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

    eval `scriptsettings.sh`
    

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

    不可能。子进程继承父进程环境的副本;它们不会影响家长的环境。如果要将数据从shell脚本传递到调用进程,最简单的方法是通过其输出。谢谢,我将尝试一下。