2016-02-04 49 views
3

我正在嘗試編寫一個shell腳本來自動執行我的開發環境設置(安裝python,nvm,node,mongo等)。我正在使用nvm來安裝Node。它告訴您關閉並重新打開您的終端以開始使用nmv命令。我試圖源代碼.bashrc和.profile使命令立即可用,因此我可以繼續使用nvm install運行腳本,但它不起作用。不能在bash腳本中使用nvm

這裏是我的腳本與安裝NVM /節點的段:已安裝

#install nvm and latest node version 
# sourcing profile and bashrc is not working here. nvm does not execute the next two lines to install node. 

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash 
source ~/.profile 
source ~/.bashrc 
nvm install 5.0 
nvm alias default node 

我得到這些消息,但請注意,我已經運行腳本和NVM /節點和工作。我也可以在完成之後運行腳本的同一終端中使用nvm和節點。它在腳本中不起作用。

=> Downloading nvm from git to '/home/myDir/.nvm' 
=> fatal: destination path '/home/myDir/.nvm' already exists and is not an empty directory. 
fatal: Not a git repository (or any of the parent directories): .git 
fatal: Not a git repository (or any of the parent directories): .git 

=> Source string already in /home/myDir/.bashrc 
=> Close and reopen your terminal to start using nvm 
./install-programs.sh: line 27: nvm: command not found 
./install-programs.sh: line 28: nvm: command not found 

回答

3

這是什麼爲我工作。在你的腳本

$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash 

然後,加載您的個人資料如下:

. ~/.nvm/nvm.sh 
. ~/.profile 
. ~/.bashrc 

並與一些運氣nvm應變得可用使用SSH或控制檯

首先安裝NVM(曾經和另發)在腳本內部。

nvm install 4.4.2 

田田!

+0

我把那在我的腳本和瞧,頂部的東西,完美。加入 – Andy

+0

。 〜/ .nvm/nvm.sh之前使用它爲我工作,謝謝! – tylermadison

1

這個劇本對我來說工作得很好:

#!/usr/bin/env bash 

if [ ! -d ~/.nvm ]; then 

    curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash 
    source ~/.nvm/nvm.sh 
    source ~/.profile 
    source ~/.bashrc 
    nvm install 5.0 
    npm install 
    npm run front 
fi 
0

,如果你對主shell中運行NVM,你只需要添加:

export NVM_DIR=$HOME/.nvm; 
source $HOME/.nvm/nvm.sh; 

在腳本