【发布时间】:2013-10-27 14:29:40
【问题描述】:
我使用 shell 脚本来配置我的服务器。修改 .bashrc 文件后,我需要退出然后重新登录以重新启动 shell。
su vagrant <<'EOF'
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=/var/www" >> ~/.bashrc
echo "alias mkvirtualenv='mkvirtualenv --no-site-packages'" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
// this is where I need help, i need to exit the shell and relogin. then run mkvirutalenv command.
mkvirtualenv test1
EOF
更新:
这是 vagrant 将运行的 shell 脚本文件。
#!/usr/bin/env bash
if [ -f "/var/vagrant_provision" ]; then
exit 0
fi
echo "Installing Flask environment and setting it up.."
echo "------------------------------------------------"
apt-get update >/dev/null 2>&1
echo "1. update is done"
#apt-get upgrade -y >/dev/null 2>&1
echo "2. upgrade is done -- skipped for dev"
rm -rf /var/www
ln -fs /vagrant /var/www
echo "3. Symbolic link is created"
apt-get install -y build-essential python-dev >/dev/null 2>&1
apt-get install -y curl >/dev/null 2>&1
echo "4. curl is installed"
apt-get install -y python-pip >/dev/null 2>&1
echo "5. pip is installed"
pip install virtualenv virtualenvwrapper >/dev/null 2>&1
echo "6. virtualenv and virtualenvwrapper are installed"
su vagrant <<'EOF'
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=/var/www" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
echo "alias mkv='mkvirtualenv --no-site-packages'" >> ~/.bashrc
echo "alias mycmd='ls'" >> ~/.bashrc
source ~/.bashrc
mycmd
mkv test1
EOF
echo "7. add environment variables to .bashrc"
echo "8. source .bashrc"
echo "9. test1 environment is created"
touch /var/vagrant_provision
echo "------------------------------------------------"
echo "Installation is done"
这是我得到的输出。仍然找不到命令。
Installing Flask environment and setting it up..
------------------------------------------------
1. update is done
2. upgrade is done -- skipped for dev
3. Symbolic link is created
4. curl is installed
5. pip is installed
6. virtualenv and virtualenvwrapper are installed
bash: line 8: mycmd: command not found
bash: line 9: mkv: command not found
7. add environment variables to .bashrc
8. source .bashrc
9. test1 environment is created
------------------------------------------------
Installation is done
【问题讨论】:
-
为什么要退出然后重新登录重新启动shell?您正在使用 source 命令。所以所有的变化都会得到反映。如果你真的想使用新的 shell,你可以使用 bash mkvirtualenv test1。
-
@virus 我必须退出并重新登录。否则,我将收到此错误消息“mkvirtualenv: command not found”,并且“bash mkvirtualenv test1”没有工作。
-
#!/usr/bin/sh echo "alias mycmd='ls'" >> ~/.bashrc source ~/.bashrc mycmd 我正在运行它,它工作正常。为什么不是你的情况?
-
我确定设置路径有问题。做一件事来调试它。将“alias mkvirtualenv”更改为“alias myvirtual”。然后看看会发生什么?
标签: bash shell ubuntu ubuntu-12.04