how to use rvm in shell scripting

2019-04-05 02:28发布

问题:

is it possible to load rvm in shell script . can any one give an example of it. when i try a shell script . i am using ubuntu system

#!/bin/bash
rvm use 1.9.3

it gives me error

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.

回答1:

You can just include rvm in your script by doing:

source ~/.rvm/scripts/rvm

then rvm should be available to your script,



回答2:

for simply executing a command against a RVM managed Ruby (or system Ruby) do :

rvm 1.9.3 exec camper_van

this assumes rvm use 1.9.3 and gem install camper_van happened previously which provides the camper_van executable

NOTE: RVM is expected to be loaded already - which it usually is for scripts started under your user (the RVM is not a function, selecting rubies with 'rvm use ...' confirms it's there already).



回答3:

# Load RVM into a shell session *as a function*
# Loading RVM *as a function* is mandatory
# so that we can use 'rvm use <specific version>'
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
  # First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"
  echo "using user install $HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
  # Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"
  echo "using root install /usr/local/rvm/scripts/rvm"
else
  echo "ERROR: An RVM installation was not found.\n"
fi


标签: bash rvm