简体   繁体   English

Shell进入zsh并从bash脚本执行命令

[英]Shell out into zsh and execute commands from bash script

I'm trying my hand a little virtualisation, so I've been using vagrant to provision a centos7 vm and now I am configuring it with applications. 我正在尝试一点虚拟化,所以我一直在使用vagrant来配置centos7 vm,现在我正在使用应用程序进行配置。

My vagrant config runs a bootstrap.sh file which is a bash script, in it I install zsh, then I want to configure it as per https://github.com/sorin-ionescu/prezto . 我的vagrant配置运行一个bootstrap.sh文件,这是一个bash脚本,在其中我安装zsh,然后我想根据https://github.com/sorin-ionescu/prezto配置它。 step 1 is launch zsh then run some commands. 第1步是启动zsh然后运行一些命令。

This is the code I have at the moment. 这是我目前的代码。

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

I want this to be the default shell when I ssh into the VM. 当我进入虚拟机时,我希望这是默认的shell。 It does not appear to be working, there are no errors with the vagrant up step so I just want to know if this is possible and if this seems correct? 它似乎没有工作,流浪者上升步骤没有错误,所以我只想知道这是否可能,如果这似乎是正确的?

Thanks 谢谢

exec zsh replaces your shell with zsh -- but doesn't in any way tell that instance of zsh to pick up at the same point in execution, or even to run the same script that the bash invocation was previously executing at all. exec zshexec zsh替换你的shell - 但是不以任何方式告诉zsh的实例在执行的同一时刻获取,甚至不运行bash调用先前执行的相同脚本。

One easy fix is to feed the rest of your script to zsh via a heredoc: 一个简单的解决方法是通过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 replaces the running script with a new zsh process. exec /usr/bin/zsh用新的zsh进程替换正在运行的脚本。 Everything after that is not executed. 之后的一切都没有被执行。

You have essentially two options: 你基本上有两个选择:

  1. Put the stuff specific to zsh into a separate script and run that with /usr/bin/zsh : 将特定于zsh的内容放入单独的脚本中并使用/usr/bin/zsh

    vagrant: 流浪汉:

     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 : /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 
  2. Modify the script so that zsh is not needed for the installation: 修改脚本,以便安装不需要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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM