在 CentOS 6.2 上的 bash 腳本中使用 rvm

在 CentOS 6.2 上的 bash 腳本中使用 rvm

我正在嘗試編寫一個腳本來完成在 CentOS 6.2 上安裝和設定 Graylog2 伺服器和 Web 介面的所有步驟。這是我遇到問題的部分:

curl -L get.rvm.io | bash -s stable
# 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 install 1.9.3 --create
rvm use 1.9.3 --default

因此,當它嘗試將rvm函數作為函數載入到shell中時,它將無法運作,導致它無法安裝ruby。我嘗試過將函數載入到 shell 中的其他方法,如下所示:

從根安裝手動取得腳本文件

source /usr/local/rvm/scripts/rvm

手動新增 PATH 環境變數以包含 rvm:

PATH=$PATH:/usr/local/rvm/bin
export PATH

手動呼叫rvm指令的路徑

/usr/local/rvm/bin/rvm install 1.9.3 --create

購買 /etc/profile.d/rvm.sh

source /etc/profile.d/rvm.sh

請注意,如果我關閉 shell 並重新登錄,rvm 加載得很好,但我需要能夠在這個腳本中完成所有操作,而無需註銷並重新登錄。你可以完整地查看我到目前為止寫的腳本這裡

任何幫助表示讚賞。

編輯

新增到腳本後set -x,以下是嘗試載入函數時的輸出:

+ [[ -s /root/.rvm/scripts/rvm ]]
+ [[ -s /usr/local/rvm/scripts/rvm ]]
+ source /usr/local/rvm/scripts/rvm
++ [[ :braceexpand:hashall:interactive-comments:posix:xtrace: =~ :posix: ]]
++ return 0
+ rvm install 1.9.3 --create
./rvm_test.sh: line 27: rvm: command not found
+ rvm use 1.9.3 --default
./rvm_test.sh: line 28: rvm: command not found

編輯2

我查看了 /usr/local/rvm/scripts/rvm 檔案並在一開始就發現了這個:

# Do not allow sourcing RVM in `sh` - it's not supported
# return 0 to exit from sourcing this script without breaking sh
[[ ":$SHELLOPTS:" =~ ":posix:" ]] && return 0 || true

所以我相信這是導致我的問題的原因,但我不確定。我在 bash 腳本編寫方面沒有經驗:\ 所以我猜問題是我的 shell 不受支援(?)任何人都可以澄清嗎?

答案1

回答我自己的問題哈哈

只需切換第一行

#!/bin/sh

#!/bin/bash

在我的腳本中解決了我遇到的問題。我發現嘗試獲取的腳本不支援 sh.

相關內容