Zshell 函数中的“-a"是什么,它与我的 shell 立即退出有什么关系? [英] What is `-a` in a Zshell function and what does it have to do with my shell immediately exiting?
问题描述
我在使用 oh-my-zsh.zshrc 文件问题>.最近,我开始尝试更加小心地处理我的基本操作系统环境,所以我安装了 Python(2 和 3)和 pyenv
使用 自制软件.在尝试为 pyenv 配置自动完成功能时,我在 oh-my-zsh 中打开了 pyenv 插件.
I'm having an issue with my .zshrc
file while using oh-my-zsh. Recently, I've started trying to be more careful about mucking with my base OS environment, so I installed Python (2 and 3) and pyenv
using homebrew. While trying to configure the autocomplete for pyenv, I switched on the pyenv plugin in oh-my-zsh.
这导致我的 shell 在启动期间关闭.我发现我可以通过注释掉 pyenv oh-my-zsh 插件的大部分活动部分来防止这种情况发生,我不确定为什么这会导致 shell 退出.
This resulted in my shell shutting down during the launch. I found that I could prevent this from happening by commenting out most of the active portion of the pyenv oh-my-zsh plugin, and I'm not sure why that's causing the shell to exit.
为了使这个问题尽可能简洁,我想知道以下函数的作用:
To make this question as concise as possible, I'd like to know what the following function does:
if [ -d $pyenvdir/bin -a $FOUND_PYENV -eq 0 ]
插件的完整代码如下:
_homebrew-installed() {
type brew &> /dev/null
}
_pyenv-from-homebrew-installed() {
brew --prefix pyenv &> /dev/null
}
FOUND_PYENV=0
pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv")
if _homebrew-installed && _pyenv-from-homebrew-installed ; then
pyenvdirs=($(brew --prefix pyenv) "${pyenvdirs[@]}")
fi
for pyenvdir in "${pyenvdirs[@]}" ; do
if [ -d $pyenvdir/bin -a $FOUND_PYENV -eq 0 ] ; then
FOUND_PYENV=1
export PYENV_ROOT=$pyenvdir
export PATH=${pyenvdir}/bin:$PATH
eval "$(pyenv init --no-rehash - zsh)"
function pyenv_prompt_info() {
echo "$(pyenv version-name)"
}
fi
done
unset pyenvdir
if [ $FOUND_PYENV -eq 0 ] ; then
function pyenv_prompt_info() { echo "system: $(python -V 2>&1 | cut -f 2 -d ' ')" }
fi
据我所知,它是这样的:
From what I can tell, it goes something like this:
- 检查 pyenvdirs (
-d $pyenvdir/bin
) 之一中是否有bin
目录,????(-a
),检查我们之前是否已经找到了pyenv
($FOUND_PYENV -eq 0
).
- Check to see if there is a
bin
directory in one of the pyenvdirs (-d $pyenvdir/bin
), ???? (-a
), check to see if we already foundpyenv
previously ($FOUND_PYENV -eq 0
).
我尝试搜索 zsh 文档,但我无法弄清楚 -a
在做什么.它像 AND 语句一样简单吗?如果是这样,为什么我的外壳会崩溃?有没有一种简单的方法将 shell 输出推送到日志文件(在 OS X 上),或者这已经完成了,我只是不知道在哪里看?
I tried searching through the zsh documentation, but I can't figure out what the -a
is doing. Is it as simple as behaving as an AND statement? If so, why is my shell crashing? Is there an easy way to push the shell output to a log file (on OS X), or is this already done and I just don't know where to look?
推荐答案
-a
有什么作用?
这里的 -a
确实意味着 AND.
What does -a
do?
Here -a
does indeed mean AND.
您没有在 zsh 文档中找到它的原因是使用了 [
内置(又名 test
;它not不鼓励使用 条件表达式(被 [[
和 ]]
包围).
The reason you did not find this in the zsh documentation is that the use of the [
builtin (aka test
; it is not part of the zsh syntax) is discouraged in favour of conditional expressions (which are surrounded by [[
and ]]
).
这是zshbuiltins(1)
的相关部分:
[ [ arg ... ] ]
[ [ arg ... ] ]
喜欢test的系统版本.添加兼容性;使用条件表达式代替 [...]
Like the system version of test. Added for compatibility; use contitional expressions instead [...]
要查找有关 [
的参数 -a
的文档,请查看 test(1)
:
To find documentation on the parameter -a
of [
have a look at test(1)
:
EXPRESSION1 -a EXPRESSION2
EXPRESSION1 -a EXPRESSION2
EXPRESSION1 和 EXPRESSION2 都为真
both EXPRESSION1 and EXPRESSION2 are true
-a
行会发生什么?
这意味着这一行
What happens in the line with -a
?
That means that this line
if [ -d $pyenvdir/bin -a $FOUND_PYENV -eq 0 ]
首先检查 $pyenvdir/bin
是否存在并且是一个目录,然后检查 $FOUND_PYENV
是否等于 0
.如果两者都为真,则执行以下块.
First checks if $pyenvdir/bin
exists and is a directory, than it checks if $FOUND_PYENV
is equal to 0
. If both are true the following block is executed.
这一行没有理由立即导致 shell 退出.
There is no reason why this line should immediatelly lead to the shell exiting.
所有 shell 输出都到终端,所以你可以在启动它时重定向它.当您在初始化期间查找错误消息时,我建议您执行以下步骤:
All shell output goes to the terminal, so you could just redirect it when starting it. As you are looking for error messages during initialisation, I'd suggest the following procedure:
- 禁用有问题的配置
- 打开终端
- 检查
SHLVL
的值:echo $SHLVL
- 重新启用配置
- 使用
zsh 2> 从正在运行的 shell 中启动一个新的 z-shell;zsh-error.log
,这会将 stderr 重定向到文件zsh-error.log". - 再次检查
SHLVL
的值.如果它大于之前的值,则退出当前 shell (exit
).(解释如下) - 查看当前目录中的zsh-error.log".
- Disable the problematic configurations
- Open a terminal
- Check the value of
SHLVL
:echo $SHLVL
- Re-enable the configurations
- Start a new z-shell from within the running shell with
zsh 2> zsh-error.log
, this redirects stderr to the file 'zsh-error.log'. - Check the value of
SHLVL
again. If it is bigger then previous value then exit the current shell (exit
). (Explanation below) - Have a look at 'zsh-error.log' in the current directory.
如果 'zsh-error.log' 没有显示任何内容,您可能需要运行 zsh -x 2>zsh-error.log
在第 5 步代替.这提供了 zsh 所做的任何事情的完整调试输出.这可能会变得非常巨大.
If 'zsh-error.log' does not show anything, you may want to run zsh -x 2> zsh-error.log
in step 5 instead. This provides a complete debug output of anything zsh does. This can get quite huge.
当 shell 启动时,它会查看环境中是否设置了 SHLVL
.如果是,则增加值,否则初始化 SHLVL
(通常使用 1
).如果你的 shell 在步骤 5 中成功启动,SHLVL
应该增加.在这种情况下,您应该停止 shell 以保持错误日志中的输出量较低.另一方面,如果 SHLVL
没有改变,shell 会自行终止,您将返回到步骤 2 中终端提供的原始 shell.
When a shell starts it looks if SHLVL
is set on the environment. If so, it increases the value, else it initalizes SHLVL
(usually with 1
). If your shell started successfully in step 5, SHLVL
should be increased. In that case you should stop the shell in order to keep the amount of output in the error log low. On the other hand, if SHLVL
is unchanged, the shell terminated on its own and you are back in the original shell provided by the terminal in step 2.
这篇关于Zshell 函数中的“-a"是什么,它与我的 shell 立即退出有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!