1

我使用 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
4

1 回答 1

3

==> 修改 .bashrc 文件后,我需要退出然后重新登录以重新启动 shell。

无需重新启动外壳。如果您希望更改立即反映在当前会话中,则可以使用以下命令执行此操作。

source ~/.bashrc

或者

. ~/.bashrc

通过这样做,您会将当前的新设置加载到您的会话中。所以你不需要重新登录。

请找到一个可以正常工作的示例代码。

#!/usr/bin/sh
echo "alias mycmd='ls'" >> ~/.bashrc
source ~/.bashrc
mycmd

解决你的问题 -->

请为用户 'vagrant' 创建无密码 ssh。请在此处查看文档以创建无密码 ssh 。

然后像下面这样输入你的运行文档命令​​。

ssh vagrant@localhost "alias mycmd='echo $HOME';/mycmd"

这里在 mycmd 之前使用 '/' 是强制性的,否则 'mycmd' 将仅由当前 shell 执行,你会得到command not found错误。

于 2013-10-27T14:53:05.373 回答