shell 如何在YAML中将多行命令包含在until-do循环中

vhmi4jdf  于 2023-02-19  发布在  Shell
关注(0)|答案(1)|浏览(145)

我需要在YAML文件的“script”部分中使用一个until-do循环,until子句中的命令在视觉上受益于被分成多行:

- >
  helm upgrade $(...) /helm/charts/ -f .(...)
    --install
    --set host=(...)
    --set tag=(...)
    --set image=(...)
    --namespace (...)

我尝试过使用这种格式将helm upgrade命令包含在一个until-do循环中:

- >
      until helm upgrade $FOO helm/charts/ -f ./(...)
        --install
        --set imagePullSecret=(...)
        --set host=(...) --set tag=(...)
        --set image=(...)
        --namespace (...); do
          helm uninstall $FOO
          done

...导致错误:

(...): line 161: --install: command not found
(...): line 162: --set: command not found
(...): line 163: --set: command not found
(...): line 164: --set: command not found
(...): line 165: --namespace: command not found
Error: uninstall: Release not loaded: (...): release: not found

然而,如果我放弃努力使这一点更可读:

- >
   until helm upgrade foo helm/charts/ -f ./(...) --install --set imagePullSecret=(...) --set host=(...) --set tag=(...) --set image=(...) --namespace (...); do
        helm uninstall foo
        done

...命令按预期运行:

Release "bar" has been upgraded. Happy Helming!

在YAML中,将多行命令包含在until-do循环中的正确方法是什么?

4dc9hkyq

4dc9hkyq1#

YAML中的文本块标量(以>开头)会将行尾折叠到连续非空行之间的空格中...除非其中一行的缩进比标量的基本缩进多。
它被设计成

>
  lorem ipsum
  dolor sit amet

   * one
   * two

将被解析为

"lorem ipsum dolor sit amet\n\n *one\n *two\n"

因为*符号比lorem ipsum行缩进更多。
在您的情况下,这意味着如果您想将命令分成多行,但又想将其解析为单行,则不能缩进续行:

- >
      until helm upgrade $FOO helm/charts/ -f ./(...)
      --install
      --set imagePullSecret=(...)
      --set host=(...) --set tag=(...)
      --set image=(...)
      --namespace (...); do
          helm uninstall $FOO
      done

您可以缩进helm uninstall,因为它应该作为单独的命令执行。
如果你不喜欢这样并且想缩进续行,你需要转义结果换行符:

- >
      until helm upgrade $FOO helm/charts/ -f ./(...) \
        --install \
        --set imagePullSecret=(...) \
        --set host=(...) --set tag=(...) \
        --set image=(...) \
        --namespace (...); do
          helm uninstall $FOO
      done

相关问题