From: "ara.t.howard" Date: 2007-06-14T23:30:51+09:00 Subject: Re: using ` and the bash source command On Jun 10, 2007, at 9:56 PM, Kevin Jackson wrote: > Hi, > >> If you were to run your Java program itself from ruby, I would think >> that you could set the JAVA_HOME environment variable directly with >> ENV[JAVA_HOME] = whatever you want, before executing Java as a >> child process of the Ruby program. > > I cannot run the Java code from the ruby program as I could be running > one of several apps. > > I actually got the behaviour I wanted by writing to .bashrc instead of > .bash_profile, so now my workflow is: > - run jdk_switcher -> open terminal -> run cli java app > instead of: > - vi .bashrc -> modify JAVA_HOME -> open new terminal -> run cli > java app. > > It's a hell of a lot quicker than manually editing each time and I'm > fairly happy with the results and there's no need for the source > command at all as the .bashrc is read every time I open a new terminal > > Anyway, thanks for the help/suggestions > > Kev kevin- you can do all of this and more with session. the fundemental difference with session is that it uses the SAME bash process for all commands, therefore any environments commands, and even bash builtins will all works. for example: cfp:~ > cat a.rb require 'time' require 'rubygems' require 'session' ### sudo gem install session ### we'll use this single bash instance for everthing bash = Session::Bash.new home = File.expand_path '~' Dir.chdir home do 4.times do ### alter the .bashrc, setting an environment var open('.bashrc', 'a+') do |fd| lines = fd.readlines lines.delete_if{|line| line =~ %r/^export FOO=/} lines << "export FOO=#{ Time.now.iso8601(2) }" fd.write lines end ### source it, now bash will pick up the environment var bash.execute 'source .bashrc' ### run a command that uses the environment, this could be java out, err = bash.execute 'echo "FOO: $FOO"' puts out end end cfp:~ > ruby a.rb FOO: 2007-06-14T08:27:22.90-06:00 FOO: 2007-06-14T08:27:22.93-06:00 FOO: 2007-06-14T08:27:22.96-06:00 FOO: 2007-06-14T08:27:22.98-06:00 -a -- http://drawohara.tumblr.com we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama