[英]Shell out into zsh and execute commands from bash script
我正在尝试一点虚拟化,所以我一直在使用vagrant来配置centos7 vm,现在我正在使用应用程序进行配置。
我的vagrant配置运行一个bootstrap.sh文件,这是一个bash脚本,在其中我安装zsh,然后我想根据https://github.com/sorin-ionescu/prezto配置它。 第1步是启动zsh然后运行一些命令。
这是我目前的代码。
echo "Installing zsh"
yum install -y zsh
echo "Configure zsh"
# touch /home/vagrant/.zshrc
exec /usr/bin/zsh
git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done
chsh -s /bin/zsh
当我进入虚拟机时,我希望这是默认的shell。 它似乎没有工作,流浪者上升步骤没有错误,所以我只想知道这是否可能,如果这似乎是正确的?
谢谢
exec zsh
用exec zsh
替换你的shell - 但是不以任何方式告诉zsh的实例在执行的同一时刻获取,甚至不运行bash调用先前执行的相同脚本。
一个简单的解决方法是通过heredoc将其余脚本提供给zsh:
/usr/bin/zsh <<'EOF'
git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done
chsh -s /bin/zsh
EOF
exec /usr/bin/zsh
用新的zsh进程替换正在运行的脚本。 之后的一切都没有被执行。
你基本上有两个选择:
将特定于zsh的内容放入单独的脚本中并使用/usr/bin/zsh
:
流浪汉:
echo "Installing zsh" yum install -y zsh echo "Configure zsh" # touch /home/vagrant/.zshrc git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto" # run external script /usr/bin/zsh /home/vagrant/install-prezto.zsh chsh -s /bin/zsh
/home/vagrant/install-prezto.zsh
:
setopt EXTENDED_GLOB for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}" done
修改脚本,以便安装不需要zsh :
echo "Installing zsh" yum install -y zsh echo "Configure zsh" # touch /home/vagrant/.zshrc git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto" # use find instead of *zsh* to link files find "${ZDOTDIR:-$HOME}/.zprezto/runcoms/" -maxdepth 1 -type f -! -name README.md --exec ln -s {} "${ZDOTDIR:-$HOME}/" + chsh -s /bin/zsh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.