所以今天晚上的大部分時間通過RVM永久影響Linux環境中活躍的Ruby版本,我一直在努力用bash腳本我寫,以幫助簡化/自動化/引導(無論你想調用它)我的開發環境,從頭開始創建一個新的Rails項目。無法shell腳本的內部時,命令執行
我正在模塊化編寫腳本,即編寫完全自己的腳本,這些腳本獨立於較大的整體腳本運行,然後將其整合到腳本流程中。
我與之戰鬥的當前scriptlet與交互式更改活動Ruby版本有關,如果您想要安裝的版本已經存在,並且如果您想要的版本不存在,它將安裝並設置爲活動版本。
這主要是一個增強整個過程,而不是在所有的腳本本身重要,但一個非常大的「錦上添花」,
至於是什麼,我在準備寫作已經完成此功能:
- 設置我的終端接受殼登錄(Linux Mint的17.1)
- 添加源$ HOME/.rvm /腳本/ RVM使RVM爲腳本中的函數運行
首先,我將粘貼代碼測試的輸出日誌,然後粘貼腳本代碼本身。注意:$ crver只是我創建的一個環境變量,它指向可執行腳本以節省輸入時間。
安裝Ruby的新版本:
[email protected] ~ $ $crver
rvm rubies
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
Is the version of Ruby you wish to use listed above? (Y/N) n
Enter the Ruby version to install (ex: 2.2.2), followed by [ENTER]: 2.2.1
Searching for binary rubies, this might take some time.
No binary rubies available for: mint/17.1/x86_64/ruby-2.2.1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for mint.
Requirements installation successful.
Installing Ruby from source to: /home/gabrial/.rvm/rubies/ruby-2.2.1, this may take a while depending on your cpu(s)...
ruby-2.2.1 - #downloading ruby-2.2.1, this may take a while depending on your connection...
ruby-2.2.1 - #extracting ruby-2.2.1 to /home/gabrial/.rvm/src/ruby-2.2.1....
ruby-2.2.1 - #applying patch /home/gabrial/.rvm/patches/ruby/2.2.1/fix_installing_bundled_gems.patch.
ruby-2.2.1 - #configuring.........................................................
ruby-2.2.1 - #post-configuration..
ruby-2.2.1 - #compiling...............................................................................
ruby-2.2.1 - #installing.............................
ruby-2.2.1 - #making binaries executable..
ruby-2.2.1 - #downloading rubygems-2.4.6
ruby-2.2.1 - #extracting rubygems-2.4.6.....
ruby-2.2.1 - #removing old rubygems.........
ruby-2.2.1 - #installing rubygems-2.4.6.....................
ruby-2.2.1 - #gemset created /home/gabrial/.rvm/gems/[email protected]
ruby-2.2.1 - #importing gemset /home/gabrial/.rvm/gemsets/global.gems...............................................
ruby-2.2.1 - #generating global wrappers........
ruby-2.2.1 - #gemset created /home/gabrial/.rvm/gems/ruby-2.2.1
ruby-2.2.1 - #importing gemsetfile /home/gabrial/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.2.1 - #generating default wrappers........
ruby-2.2.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.2.1 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri
Using /home/gabrial/.rvm/gems/ruby-2.2.1
[email protected] ~ $ rvm list
rvm rubies
ruby-2.2.1 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
使用Ruby的預先存在的版本,並將其設置爲活動:
[email protected] ~ $ $crver
rvm rubies
ruby-2.2.1 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
Is the version of Ruby you wish to use listed above? (Y/N) y
Enter the Ruby version to use (ex: 2.2.2), followed by [ENTER]: 2.2.1
Using /home/gabrial/.rvm/gems/ruby-2.2.1
[email protected] ~ $ rvm list
rvm rubies
ruby-2.2.1 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
正如你所看到的,在腳本它驗證它將Ruby的版本設置爲2.2.1,但在實際環境中,它仍然爲2.2.2
奇怪的是,我也嘗試使用rvm use $rubyver --default
取得部分成功。當直接在命令行中運行的rvm list
,它顯示了默認版本預期的符號,但紅寶石的活動版本保持不變:
[email protected] ~ $ $crver
rvm rubies
ruby-2.2.1 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
Is the version of Ruby you wish to use listed above? (Y/N) y
Enter the Ruby version to use (ex: 2.2.2), followed by [ENTER]: 2.2.1
Using /home/gabrial/.rvm/gems/ruby-2.2.1
[email protected] ~ $ rvm list
rvm rubies
* ruby-2.2.1 [ x86_64 ]
=> ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
# * - default
下面是腳本本身的代碼塊:
#!/bin/bash
source $HOME/.rvm/scripts/rvm
rvmexec() {
msg="Enter the Ruby version to $1 (ex: 2.2.2), followed by [ENTER]: "
case $1 in
[use]*)
read -p "$msg" rubyver
eval rvm use $rubyver
unset rubyver;
break;;
[install]*)
read -p "$msg" rubyver
eval rvm install $rubyver && rvm use $rubyver
unset rubyver;
break;;
esac
}
rvm list
while true; do
read -p "Is the version of Ruby you wish to use listed above? (Y/N) " rubyverexist
case $rubyverexist in
[Yy]*) rvmexec use; break;;
[Nn]*) rvmexec install; break;;
*) echo -e "\nERROR: Please enter a valid response\n";;
esac
done
任何幫助,將不勝感激,請原諒我提前如果我留下什麼東西出來的關鍵,這是上午01時13分和下班6點:)
乾杯!
有大量的信息通過這裏來看看,我沒有時間去仔細閱讀這一切,但只給定標題和介紹我要去猜測,問題是,你是期待對環境變量/等的更改。由腳本完成(通過'eval'和'rvm')來影響運行腳本的運行shell的環境。這是不可能的。這不是如何工作。這就是爲什麼像'rvm'這樣的東西輸出環境變量爲字符串,並告訴你'eval'他們做出這些變化。如果需要,也可以從腳本中完成。 –