.zshrc在$上无法识别的条件 -

时间:2014-02-25 20:05:16

标签: zsh zshrc

我正在尝试将旧的.bashrc中的某些功能移植到我的.zshrc中,并且我遇到了在bash中运行的条件的问题。

每当我远程登录到我的计算机时,我都会检查$-变量以查看它是否是交互式的。如果是的话,我会启动一个emacs服务器,如果还没有运行并更改到我的代码目录。否则(例如,如果我得到一个带scp的文件),我什么都不做。

以下是代码:

if [[ $- -regex-match "i" ]]; then
    ps -u myusername | grep emacs > /dev/null
    if [ $? -eq 0 ]; then
        echo "emacs server already running"
    else
        emacsserver
    fi

    aliastocdtomydirectory
fi

这是zsh给我的错误:.zshrc:125: unrecognized condition: $ - '``

使用$-时有谁知道如何解决此错误?我试过引用它,将它包装在$(echo $-)但没有一个有用。提前谢谢。

修改:如果我将代码切换为:

if [[ $- =~ "i" ]]; then
    ps -u myusername | grep emacs > /dev/null
    if [ $? -eq 0 ]; then
        echo "emacs server already running"
    else
        emacsserver
    fi

    aliastocdtomydirectory
fi

我现在得到:.zshrc:125: condition expected: =~我不确定zsh在这里错误地解释了什么,因为我不太熟悉zsh的shell脚本的语义。有人能指出我如何在zsh中表达这种情况的正确方向吗?

2 个答案:

答案 0 :(得分:2)

zsh中,您不需要打扰$-,我认为这主要是为了兼容POSIX。

if [[ -o INTERACTIVE ]]; then
    if ps -u myusername | grep -q emacs; then
        echo "emacs server already running"
    else
        emacsserver
    fi

    aliastocdtomydirectory
fi

答案 1 :(得分:1)

-regex-match仅在加载模块zsh/regexman 1 zshmodules)时可用。 (错误消息取决于版本:如果未在4.3.17上加载zsh: unknown condition: -regex-match,在4.3.10上加载zsh:1: unknown condition: -$-,我得到[[ $- =~ "i" ]]

您可以尝试{{1}},但不依赖于其他模块。