Zsh 无此类事件:1

Zsh 无此类事件:1,zsh,Zsh,我有以下$HOME/.zshrc文件: [vagrant@devel]/vagrant% cat ~/.zshrc #!/usr/bin/env zsh # BEGIN ANSIBLE MANAGED BLOCK if [ $(history | wc -l) -eq 0 ]; then # we've just shelled in; "magically" cd into the vagrant shared folder cd "/vagrant" fi # END ANSIBLE

我有以下
$HOME/.zshrc
文件:

[vagrant@devel]/vagrant% cat ~/.zshrc
#!/usr/bin/env zsh
# BEGIN ANSIBLE MANAGED BLOCK
if [ $(history | wc -l) -eq 0 ]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK
我在Bash中使用了相同的脚本,效果很好;初次登录时,用户没有任何历史记录,并被神奇地传送到
/vagrant

当我使用此
$HOME/.zshrc
登录到此框时,我看到以下错误:

/home/vagrant/.zshrc:fc:3: no such event: 1
[vagrant@devel]/vagrant%
我不知道这意味着什么,谷歌也没有给我带来结果。显然代码是有效的,但这似乎是某种错误


有什么想法吗?

显然,
zsh
history
命令在没有历史记录时会发出错误,因此:

#!/usr/bin/env zsh
# BEGIN ANSIBLE MANAGED BLOCK
if [ $(history 2>/dev/null | wc -l) -eq 0 ]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK

没有更多错误。

显然,
zsh的
history
命令在没有历史记录时发出错误,因此:

#!/usr/bin/env zsh
# BEGIN ANSIBLE MANAGED BLOCK
if [ $(history 2>/dev/null | wc -l) -eq 0 ]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK

没有更多错误。

您不需要调用
历史记录
内置命令并计算行数

您只需在
~/.zshrc
中检查
HISTCMD
变量是否为零即可
HISTCMD
表示历史记录中的当前命令序列号

因此,您的
~/.zshrc
可以是:

# BEGIN ANSIBLE MANAGED BLOCK
if [[ $HISTCMD -eq 0 ]]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK

您不需要调用
history
builtin命令并计算行数

您只需在
~/.zshrc
中检查
HISTCMD
变量是否为零即可
HISTCMD
表示历史记录中的当前命令序列号

因此,您的
~/.zshrc
可以是:

# BEGIN ANSIBLE MANAGED BLOCK
if [[ $HISTCMD -eq 0 ]]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK
你不需要
#/usr/bin/env zsh
~/.zshrc
中,您不需要
#/usr/bin/env zsh
中的
~/.zshrc