James Testa
James Testa

Reputation: 2921

can't access rvm from ruby script after install, trouble with source .bashrc

I am trying to install ruby 1.9.2 from a ruby script using rvm which is installed from the same script. The problem I have is sourcing .bashrc within the script so that the path to rvm is available within the script. The following works:

#!/usr/bin/env ruby

%x[bash -c "bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)" ]
%x[ln -s /usr/local/rvm/ .rvm]
%x[echo "[[ -s \"$HOME/.rvm/scripts/rvm\" ]] && source \"$HOME/.rvm/scripts/rvm\""  >> ~/.bashrc]

But I am not able to execute from within this ruby script

 source .bashrc

I've tried

  %x[bash -c "bash <(. .bashrc)"]

I've also tried running separately the line that the script added to .bashrc

 %x[ bash -c "bash <(source  \"$HOME/.rvm/scripts/rvm\" )"   ]

I have tried sessions but the puts below gives a blank response to "which rvm".

 require 'rubygems'
 require 'session'
 bash = Session::Bash.new
 stdout, stderr = bash.execute 'source .bashrc'
 puts "which rvm = " + %x[which rvm 2>&1].inspect

Am I looking at this all wrong? Is trying to source .bashrc within a ruby script and using the resulting environment to execute subsequent commands possible?

Update -

Using Ian's approach below with bash -ic I was able to get the ruby script working. But all subsequent shell commands that need to see the new environment have to be run with bash -ic as well. Here is the working script:

 #!/usr/bin/env ruby
 %x[bash -c "bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)" ]
 %x[ln -s /usr/local/rvm/ .rvm]
 %x[echo "[[ -s \"$HOME/.rvm/scripts/rvm\" ]] && source \"$HOME/.rvm/scripts/rvm\""  >> ~/.bashrc]
 %x[bash -ic "bash <(. ~/.bashrc); rvm install ruby-1.9.2-p290; rvm 1.9.2-p290 --default;"]

Upvotes: 1

Views: 650

Answers (1)

Ian Dickinson
Ian Dickinson

Reputation: 13305

I believe you need to pass the -l flag to the invocation of bash from ruby, which will make the embedded bash shell act as a login shell and read .bashrc and .bash_profile. See also the ref manual for more details.

Update

Sorry, I meant the -i flag, not -l. I tried this:

[~/temp/rubytest] 
ian@ian-desktop2 $ echo "export FOO=fubar" >> ~/.bashrc
[~/temp/rubytest] 
ian@ian-desktop2 $ irb
jruby-1.6.5 :001 > %x[ bash -c "env | grep FOO" ]
 => "" 
jruby-1.6.5 :003 > %x[ bash -ic "env | grep FOO" ]
 => "FOO=fubar\n" 
jruby-1.6.5 :004 > %x[ bash -ic "echo 'export FOO2=fubar2' >> ~/.bashrc ; env | grep FOO" ]
 => "FOO=fubar\n" 
jruby-1.6.5 :005 > %x[ bash -ic "echo 'export FOO2=fubar2' >> ~/.bashrc ; source /home/ian/.bashrc ; env | grep FOO" ]
 => "FOO=fubar\nFOO2=fubar2\n" 
jruby-1.6.5 :006 > 

Upvotes: 2

Related Questions