当命令在Shell脚本内部执行时,无法通过RVM永久影响Linux环境中的活动Ruby版本



因此,在今天晚上的大部分时间里,我一直在为我写的bash脚本而苦苦,从头开始创建新的Rails项目时。

我正在模块化编写脚本,那就是编写独立于较大整体脚本然后将其集成到脚本流中的完全自我包含的脚本。

当前我与之抗争的当前脚本,如果您想要安装的版本已经存在,则与互动更改活动ruby版本有关,如果您想要的版本不存在,它将安装并设置为Active版本。

这主要是对整个过程的增强,而对脚本本身而言并不重要,而是一个很大的"很好"

至于我已经做了编写此功能的准备工作:

  1. 将我的终端设置为接受Shell Logins(Linux Mint 17.1)

首先,我将粘贴代码测试的输出日志,然后粘贴脚本代码本身。注意:$ crver只是一个环境var i创建的,它指向可执行脚本以节省打字时的时间。

安装新版本的Ruby:

gabrial@thinkpad ~ $ $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/ruby-2.2.1@global
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
gabrial@thinkpad ~ $ rvm list
rvm rubies
   ruby-2.2.1 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
#  * - default

使用Ruby的预先存在版本并将其设置为活动:

gabrial@thinkpad ~ $ $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
gabrial@thinkpad ~ $ 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时,它显示了默认版本的预期符号,但是Ruby的活动版本保持不变:

gabrial@thinkpad ~ $ $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
gabrial@thinkpad ~ $ 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 responsen";;
        esac
    done

任何帮助都将不胜感激,如果我遗漏了任何批判性的事情,请提前原谅我,这是凌晨1:13,工作在凌晨6点出现:)

欢呼!

好吧,一组新的眼睛和Google搜索,事实证明,我要做的只是用source序言(现在是一个别名),然后脚本的行为为预期:

From ~/.profile:
alias crver="source $HOME/workspace/crver.sh"
--- Output Begins ---
gabrial@thinkpad ~ $ rvm list
rvm rubies
   ruby-2.2.1 [ x86_64 ]
=* ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
#  * - default
gabrial@thinkpad ~ $ 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
gabrial@thinkpad ~ $ rvm list
rvm rubies
=* ruby-2.2.1 [ x86_64 ]
   ruby-2.2.2 [ x86_64 ]
# => - current
# =* - current && default
#  * - default

是的!

〜fin

相关内容

  • 没有找到相关文章

最新更新