简体   繁体   中英

Unable to permanently affect active Ruby version in Linux environment via RVM when command executed inside of shell script

So for the better part of this evening, I have been struggling with a bash script I'm writing to help streamline/automate/bootstrap (whatever you want to call it) my development environment, when creating a new Rails project from scratch.

I'm writing the script modularly, that is writing fully self containing scriptlets that run independently of the larger overall script and then integrating it into the script flow.

The current scriptlet I'm fighting with, has to do with interactively changing the active Ruby version if the version you want installed already exists, and if the version you want doesn't exist it will install and set that as the active version.

This is chiefly an enhancement to the overall process, and not at all critical to the script itself, but is a very big "nice to have"

As for what I've already done in preparation of writing this functionality:

  1. Set my terminal to accept shell logins (Linux Mint 17.1)
  2. Added source $HOME/.rvm/scripts/rvm to enable rvm to run as a function within the script

First I will paste the output log of my testing of the code, and then I will paste the script code itself. Note: $crver is just an environment var I created which points to the executable script to save time on typing.

Installing a new version of 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

Using a pre-existing version of Ruby and setting it as active:

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

As you can see, in the script it verifies that it sets the version of Ruby to 2.2.1, but in the actual environment, it remains as 2.2.2

Curiously enough, I also tried using rvm use $rubyver --default to partial success. When running an rvm list directly from the command line, it shows the expected notation for the default version, but the active version of Ruby remains unchanged:

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

Here is the code block for the script itself:

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

Any help would be appreciated, forgive me in advance if I've left anything critical out, it's 1:13am and work comes at 6am :)

Cheers!

Well, a fresh set of eyes and a Google search later it turns out all I had to do was preface the var (which is now an alias) with source and then the script behaves as expected:

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

Yay!

~Fin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM