【问题标题】:zsh function case condition parse error near `)'`)' 附近的 zsh 函数 case 条件解析错误
【发布时间】:2019-10-30 02:07:38
【问题描述】:

我正在关注这个博客来设置一个 zsh 函数来切换 aws cli 配置文件 :https://mads-hartmann.com/2017/04/27/multiple-aws-profiles.html

这是博客中的zsh函数:

function aws-switch() {
    case ${1} in
        "")
        clear)
            export AWS_PROFILE=""
            ;;
        *)
            export AWS_PROFILE="${1}"
            ;;
    esac
}

#compdef aws-switch
#description Switch the AWS profile

_aws-switch() {

    local -a aws_profiles

    aws_profiles=$( \
        grep '\[profile' ~/.aws/config \
        | awk '{sub(/]/, "", $2); print $2}' \
        | while read -r profile; do echo -n "$profile "; done \
    )

    _arguments \
        ':Aws profile:($(echo ${aws_profiles}) clear)'
}

_aws-switch "$@"

当我运行 source ~/.zshrc 时,我将这些行添加到我的 ~/.zshrc 它给出 /.zshrc:4: parse error near `)' 我阅读了 zsh 函数文档,但仍然不太擅长理解语法以及如何解决这个问题。

【问题讨论】:

    标签: zsh oh-my-zsh zshrc


    【解决方案1】:

    查看 zsh 手册页 (man zshmisc):

    [[(] 模式 [|模式] ... ) list (;;|;&|;|) ] ... esac 中的case word

    如您所见,您必须用| 分隔多个模式:

    case $1 in
      |clear)
        ....
    

    【讨论】:

    • 对于C-like fall through,你也可以使用"") : ;&,它执行无操作:并以;&终止case,这导致下一个块被执行为就好像它匹配$1
    • 所以我应该把这个放在哪里 |在?我试图在你的建议和许多其他职位中明确表示,但没有一个奏效。
    • 发布您修改后的完整代码。它应该工作。我用我的 Zsh 5.5.1 版试了一下。
    猜你喜欢
    • 2017-06-10
    • 2018-10-19
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2022-11-05
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多