2017-05-30 49 views
0

我有以下$HOME/.zshrc文件:沒有這樣的事件:1

[[email protected]]/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 

我使用的是猛砸相同的腳本和它的作品就好了;在初始登錄時,用戶沒有歷史和奇蹟般地運到/vagrant

當我登錄到這個盒子與此$HOME/.zshrc,我看到以下錯誤:

/home/vagrant/.zshrc:fc:3: no such event: 1 
[[email protected]]/vagrant% 

我不知道這意味着什麼和谷歌是不是導致我的結果。顯然,代碼有效,但這似乎是某種錯誤。

任何想法?

+0

你不需要'#在/ usr/bin中/ ENV在'〜/ .zshrc' zsh' – anubhava

回答

0

顯然,zshhistory命令發出錯誤的時候沒有歷史,所以:

#!/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 

沒有更多的錯誤。

2

你不需要調用history內建命令和計數的線。

你可以只檢查HISTCMD變量在~/.zshrc爲零。 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