我正在尝试编写一个shell脚本来自动化我的开发环境设置(安装python,nvm,node,mongo等...)。我正在使用nvm来安装Node。它会告诉您关闭并重新打开终端以开始使用nmv命令。我尝试使用.bashrc和.profile来使命令立即可用,这样我就可以继续使用nvm install运行脚本了,但它不起作用。
以下是与安装NVM / Node相关的脚本部分:
#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 / Node已经安装并正常工作。我也可以在同一个终端中使用nvm和node,在完成后运行脚本。它只是在脚本中不起作用。
=> 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
答案 0 :(得分:6)
这对我有用。
首先使用SSH或控制台安装nvm(一次和单独):
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
然后在您的脚本中,按如下方式加载您的个人资料:
. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc
运气好nvm
应该可以在脚本中找到。
nvm install 4.4.2
多田!
答案 1 :(得分:2)
只需将其放在脚本顶部:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
在这里像魅力一样工作。
答案 2 :(得分: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
答案 3 :(得分:1)
如果你在主shell上运行nvm,你只需要添加:
export NVM_DIR=$HOME/.nvm;
source $HOME/.nvm/nvm.sh;
你脚本中的