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;"]
I believe you need to pass the
-lflag to the invocation of bash from ruby, which will make the embedded bash shell act as a login shell and read.bashrcand.bash_profile. See also the ref manual for more details.Update
Sorry, I meant the
-iflag, not-l. I tried this: