2011-12-05 54 views
1

我想從ruby腳本安裝ruby 1.9.2,它使用從同一腳本安裝的rvm。我遇到的問題是在腳本中採用.bashrc,以便在腳本中提供rvm的路徑。以下作品:安裝後無法從ruby腳本訪問rvm,遇到源碼問題.bashrc

#!/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] 

但我無法從這個Ruby腳本

source .bashrc 

我已經試過

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

我也試過單獨運行線路內執行腳本添加到.bashrc中

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

我已經嘗試了會議,但下面的提示給出了「哪個rvm」的空白迴應。

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

我看着這一切都錯了嗎?試圖在ruby腳本中輸入.bashrc並使用生成的環境執行後續命令?

更新 -

使用伊恩的方法在下面的bash -IC我能得到的Ruby腳本工作。但是所有需要看到新環境的後續shell命令也必須以bash -ic運行。這裏是工作的腳本:

#!/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;"] 

回答

2

我相信你需要的-l標誌傳遞到慶典的紅寶石調用,這將使得嵌入式bash shell中充當登錄shell和閱讀.bashrc.bash_profile。有關更多詳細信息,另請參閱ref manual

更新

對不起,我說的是-i標誌,不-l。我試過這個:

[~/temp/rubytest] 
[email protected] $ echo "export FOO=fubar" >> ~/.bashrc 
[~/temp/rubytest] 
[email protected] $ 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 > 
+0

伊恩 - 謝謝你的回答。你的建議是有道理的,但我仍然無法實現。我試過%x [bash -c -l「bash <(source .bashrc)」]以及'/ bin/bash -c -l'。/root/.bashrc'以及其他一些內容。你可以發佈一些代碼,你已經嘗試過從ruby腳本中執行源.bashrc並運行? –

+0

詹姆斯:我已經更新了上面的答案 –

+0

伊恩 - 非常感謝,那些石頭!看起來,對於所有後續的shell命令,如果他們想要看到新的環境,則需要使用bash -ic以及我在帖子中更新的方式運行它們。但它一切正常! –