如何在 bash 中为 mike yq v4 处理变量

问题描述

如何使用 Mike Farah 的 YQ v4 更新具有特殊字符的字段。

例如操作如下:

      containers:
        - name: flyway
          image: xx.dkr.ecr.eu-west-1.amazonaws.com/testimage:60

尝试错误

$ yq e ".spec.template.spec.containers[0].image=xx.dkr.ecr.eu-west-1.amazonaws.com/testimage:61" flyway.yaml
Error: Bad expression,please check expression Syntax

$ yq e ".spec.template.spec.containers[0].image=xx\.dkr\.ecr\.eu-west-1\.amazonaws\.com\/testimage:61" flyway.yaml
Error: Parsing expression: Lexer error: Could not match text starting at 1:53 failing at 1:54.
        unmatched text: "\\"

#########

$ echo $image
xx.dkr.ecr.eu-west-1.amazonaws.com/testimage:61
$ yq e ".spec.template.spec.containers[0].image=$image" flyway.yaml
Error: Bad expression,please check expression Syntax

没有找到任何解释特殊字符转义字符的文档。

解决方法

找到这个文档: https://mikefarah.gitbook.io/yq/operators/env-variable-operators

这对我有用:

$ echo $image
xx.dkr.ecr.eu-west-1.amazonaws.com/testimage:61
$ myenv=$image yq e '.spec.template.spec.containers[0].image=env(myenv)' flyway.yaml
<<removed contents for brevity >>
    spec:
      containers:
        - name: flyway
          image: xx.dkr.ecr.eu-west-1.amazonaws.com/testimage:61