bash 无法通过 `rvm gemset use` 从 shell 脚本更改 RVM gemset

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5792707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:51:29  来源:igfitidea点击:

Cannot change RVM gemset from shell script via `rvm gemset use`

bashrvm

提问by Michelle Tilley

(See update at bottom)

(见底部更新)



I feel like I'm missing something terribly obvious here, but I can't change gemsets from within a shell script. This minimal script demonstrates:

我觉得我在这里遗漏了一些非常明显的东西,但我无法从 shell 脚本中更改 gemset。这个最小的脚本演示了:

#!/usr/bin/env bash

rvm gemset use "testing"

I even tried the instructions from the Scripting RVM page(although it didn't seem necessary):

我什至尝试了脚本 RVM 页面中的说明(尽管似乎没有必要):

#!/usr/bin/env bash

# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
  # First try to load from a user install
  source "$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"
else
  printf "ERROR: An RVM installation was not found.\n"
fi

rvm gemset use "testing"

Still no go.

还是不行。

Interestingly enough, if I try to run the script without first creating the "testing" gemset, I get ERROR: Gemset 'testing' does not exist, rvm gemset create 'testing' first.However, if I create the gemset and then run the script, I get no output from the script and the gemset is not changed (according to rvm info). I am able to perform otherRVM gemset actions, such as creating gemsets and trusting .rvmrcfiles, from within the script.

有趣的是,如果我尝试在没有首先创建“测试”gemset 的情况下运行脚本,我得到ERROR: Gemset 'testing' does not exist, rvm gemset create 'testing' first.但是,如果我创建了 gemset 然后运行脚本,我没有从脚本中得到输出并且 gemset 没有改变(根据rvm info)。我能够从脚本中执行其他RVM gemset 操作,例如创建 gemset 和信任.rvmrc文件。

[Update]

[更新]

Of course, the environment ischanging, as indicated by a call to rvm infofrom within the script. How do I get these changes to persist/affect the calling shell? Or, if that's not possible (as indicated here), is there any way to set the current RVM gemset based on input to a script?

当然,环境正在发生变化,正如rvm info脚本中调用from所表明的那样。如何让这些更改持久化/影响调用 shell?或者,如果那是不可能的(如所示这里),有没有什么办法来设置基于输入到一个脚本当前RVM宝石?

回答by Alex Fortuna

Had exactly the same problem, and here's the solution:

有完全相同的问题,这是解决方案:

#!/bin/bash

# IMPORTANT: Source RVM as a function into local environment.
#            Otherwise switching gemsets won't work.
[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"

# Enable shell debugging.
set -x

rvm 1.9.2@gemset_a
rvm gemdir
gem env gemdir

rvm 1.9.2@gemset_b
rvm gemdir
gem env gemdir

What I've found out is that your interactive shell has got rvm()and its helpers, whereas script's environment has notgot them. rvmbinary is executed instead, partiallyworking and thus causing some confusion.

我发现你的交互式 shell 已经得到了rvm()它的助手,而脚本的环境还没有得到它们。rvm二进制文件改为执行,部分工作,从而导致一些混乱。

回答by Michelle Tilley

I ended up implementing the functionality I wanted as a function instead of a shell script.

我最终将我想要的功能实现为函数而不是 shell 脚本。

function rvmrc {
  rvm gemset create 
  rvm gemset use 
  echo "rvm gemset use " > .rvmrc
  rvm rvmrc trust
}