将ruby变量导出到父进程

时间:2021-10-15 23:22:35

I am new to ruby. Still learning it.

我是红宝石的新手。还在学习它。

I have bash script and a ruby script, having independent responsibilities.

我有bash脚本和ruby脚本,有独立的责任。

The bash script calls the ruby script with some command line arguments. The ruby script, after parsing these command line args will set/unset some variables local to it. These variables will have the same use/name in the parent bash script.

bash脚本使用一些命令行参数调用ruby脚本。解析这些命令行参数后,ruby脚本将设置/取消设置一些局部变量。这些变量在父bash脚本中具有相同的用法/名称。

So, i need to have the modified values of the variables in the ruby script be reflected in the parent bash script also.

所以,我需要在ruby脚本中修改变量值也反映在父bash脚本中。

Couple of approaches I have thought of are:

我想到的几种方法是:

1.) Export the variables to the environment in bash, then have ruby modify them by using the ENV[] hash. In this case, there wouldn't be any need to have local variables in ruby to store these values, ruby would directly modify the imported variables from bash, as they are environment variables. However, would these changed values of the environment be reflected in the bash script?

1.)在bash中将变量导出到环境中,然后让ruby使用ENV []哈希修改它们。在这种情况下,不需要在ruby中存储局部变量来存储这些值,ruby会直接修改bash中导入的变量,因为它们是环境变量。但是,这些更改的环境值是否会反映在bash脚本中?

2.) After the ruby script returns to the parent bash script, call on some methods of ruby that will return the a variable's value to it. So for each variable, a method is called. Something like for python,

2.)在ruby脚本返回到父bash脚本之后,调用一些ruby方法,它将返回一个变量的值。因此,对于每个变量,都会调用一个方法。像python这样的东西,

python -c 'import python_module; print python_method()'

python -c'import python_module; print python_method()'

Is this possible in ruby?

红宝石有可能吗?

If none of the above two are possible, is there any other way to have the values of ruby variables be reflected back in the parent bash script?

如果上述两个都不可能,有没有其他方法可以将ruby变量的值反映回父bash脚本中?

Or, is there any scripting option other than ruby/python that can help me do this?

或者,是否有任何脚本选项,除了ruby / python可以帮助我这样做?

I Appreciate the time in reading this post and helping me out. :-)

我感谢您阅读这篇文章并帮助我的时间。 :-)

1 个解决方案

#1


1  

The simplest mechanism would be similar to the one used by the ssh-agent program.

最简单的机制类似于ssh-agent程序使用的机制。

Have your program output a set of environment export statements like

让您的程序输出一组环境导出语句,如

export THIS=something
export THAT=something_else

and then have the calling script eval the result, as in

然后让调用脚本eval结果,如

eval `my_program`

This will cause the shell to incorporate your environment variables. For example, here is a small Python program.

这将导致shell合并您的环境变量。例如,这是一个小型Python程序。

print "export THIS=something"
print "export THAT='something else'"

When I run it, it outputs the two export statements:

当我运行它时,它输出两个导出语句:

airhead:Python sholden$ python stackoflo.20140710-1.py
export THIS=something
export THAT='something else'

So when I eval the result the export statements are executed by the calling shell:

因此,当我评估结果时,导出语句由调用shell执行:

airhead:Python sholden$ eval `python stackoflo.20140710-1.py`

The variables its sets are now a part of the environment:

它的集合变量现在是环境的一部分:

airhead:Python sholden$ echo $THIS
something
airhead:Python sholden$ echo $THAT
something else

Finally, here's proof that the values do go into the shell's environment: a subshell sees them.

最后,这里证明了值进入shell的环境:子shell看到它们。

airhead:Python sholden$ bash
bash-3.2$ echo $THIS
something

#1


1  

The simplest mechanism would be similar to the one used by the ssh-agent program.

最简单的机制类似于ssh-agent程序使用的机制。

Have your program output a set of environment export statements like

让您的程序输出一组环境导出语句,如

export THIS=something
export THAT=something_else

and then have the calling script eval the result, as in

然后让调用脚本eval结果,如

eval `my_program`

This will cause the shell to incorporate your environment variables. For example, here is a small Python program.

这将导致shell合并您的环境变量。例如,这是一个小型Python程序。

print "export THIS=something"
print "export THAT='something else'"

When I run it, it outputs the two export statements:

当我运行它时,它输出两个导出语句:

airhead:Python sholden$ python stackoflo.20140710-1.py
export THIS=something
export THAT='something else'

So when I eval the result the export statements are executed by the calling shell:

因此,当我评估结果时,导出语句由调用shell执行:

airhead:Python sholden$ eval `python stackoflo.20140710-1.py`

The variables its sets are now a part of the environment:

它的集合变量现在是环境的一部分:

airhead:Python sholden$ echo $THIS
something
airhead:Python sholden$ echo $THAT
something else

Finally, here's proof that the values do go into the shell's environment: a subshell sees them.

最后,这里证明了值进入shell的环境:子shell看到它们。

airhead:Python sholden$ bash
bash-3.2$ echo $THIS
something