0

I am new to ruby. Still learning it.

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

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.

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

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?

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,

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?

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

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

Community
  • 1
  • 1
Yusuf Husainy
  • 625
  • 8
  • 19

1 Answers1

1

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

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 `my_program`

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

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:

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.

airhead:Python sholden$ bash
bash-3.2$ echo $THIS
something
holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Hi, Thanks for your reply. :-) I did try out what you said, but it didn't work. The environment variables defined in the ruby script aren't reflected in the calling bash upon running ruby script as an argument to eval in bash. On reading about eval on google, it is my understanding that it gives the return value of the called command. – Yusuf Husainy Jul 10 '14 at 08:41
  • You would want to see this link: http://stackoverflow.com/questions/2660571/exporting-an-environment-variable-in-ruby There isn't any way to have the parent process see the changes made to the env variables by the child process. In this case, parent process is the bash script's shell and the child process is the ruby script's shell. If you could provide an example using a bash and ruby script, similar to what I have described, I would be obliged. :-) – Yusuf Husainy Jul 10 '14 at 09:21
  • 1
    OK, I've edited my answer to include a simple example. – holdenweb Jul 10 '14 at 09:46
  • Thank You for the code example. It did help to an extent. However, this would work only if there are'nt any print/puts statements having their string literals other than system commands. In my requirement, I need to have lot of puts statements for error reporting and other debugging information. So, on using eval command, the system reports invalid commands. However, Really smart logic and idea that you came up with. Excellent! – Yusuf Husainy Jul 10 '14 at 10:29