简体   繁体   English

安装 Oh My Zsh 时 shell 脚本代码中的命令无法访问

[英]Unreachable command in a shell script code while installing Oh My Zsh

Here is my sample1.sh :这是我的sample1.sh

#!/bin/bash

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
echo "foo"

Output:输出:

在此处输入图像描述

Here is my sample2.sh :这是我的sample2.sh

#!/bin/bash

rm -rf ~/.oh-my-zsh
rm ~/.zshrc

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
echo "foo"

Output:输出:

在此处输入图像描述

As you see, the only difference between above snippets are below lines:如您所见,上述代码段之间的唯一区别在于以下几行:

rm -rf ~/.oh-my-zsh
rm ~/.zshrc

Question: why am I able to see foo , only when OMZ is already installed?问题:为什么只有当 OMZ 已经安装时我才能看到foo What's so specific inside https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh , that after it detects that OMZ does not exist and can be installed, after successful installation, it doesn't continue to reach my foo ? https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh里面有什么特别之处,它检测到 OMZ 不存在并且可以安装后,安装成功后,它不会' 不继续达到我的foo

Obviously, that echo "foo" was just an example to highlight the general problem.显然,那个echo "foo"只是突出一般问题的一个例子。

In the final built of the script, what I want to achieve is to simply enable some plugins once OMZ is installed by calling:在脚本的最终构建中,我想要实现的是在安装 OMZ 后通过调用简单地启用一些插件:

sed -i '' -e 's/^plugins=.*/plugins=(git, sublime)/' ~/.zshrc

This line works fine only when I trigger it manually.这条线只有在我手动触发时才能正常工作。 The problem is that it never calls sed once OMZ is installed.问题是一旦安装了 OMZ,它就不会调用sed Thanks for pointing out, where is the problem that I don't understand.感谢您指出,我不明白的问题在哪里。

Part of the install script for OMZ is to switch the current shell to sh with the line env zsh . OMZ的安装脚本的一部分是使用env zsh行将当前shell切换到sh。 I believe that this is basically causing your script to fork ZSH and never actually finish running. 我相信这基本上是导致您的脚本派生ZSH并从未真正完成运行。 If you exit from the ZSH shell then it should continue as normal. 如果您从ZSH Shell exit ,则它应继续正常运行。

Oh-my-zsh will fork the current process. Oh-my-zsh将分叉当前进程。 To fix, just cut it out of the script. 要解决此问题,只需将其从脚本中删除即可。

 sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sed "s/env zsh//g")"

作为一种解决方法,您可以使用&<\/code>并wait<\/code>并行运行安装命令并等待其完成。

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" &
wait
echo "foo"

Thanks @mgild for the idea, that was exactly what I was looking for. 感谢@mgild的想法,这正是我想要的。 Just one thing - the command should be: 只是一件事-命令应该是:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sed "s/env zsh.*//g")"

So that sed removes the whole and not just "env zsh" substring. 因此,sed会删除整个子字符串,而不仅仅是“ env zsh”子字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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