2

我正在尝试将一些功能从我的旧 .bashrc 移植到我的 .zshrc 中,但我遇到了在 bash 中有效的情况。

每当我远程登录到我的计算机时,我都会让 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 中表达这种情况的正确方向吗?

4

2 回答 2

2

zsh中,您无需为 POSIX 兼容性而烦恼$-,我认为它主要用于 POSIX 兼容性。

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

    aliastocdtomydirectory
fi
于 2014-02-25T22:33:57.493 回答
1

-regex-match仅在加载模块zsh/regex( ) 时可用。man 1 zshmodules(错误消息取决于版本:zsh: unknown condition: -regex-match如果它不是在 4.3.17 上加载,而是zsh:1: unknown condition: -$-在 4.3.10 上加载,我会得到)。

您可以尝试[[ $- =~ "i" ]]不依赖于其他模块的。

于 2014-02-25T21:55:05.530 回答