计算bash shell脚本中的json键对数

发布于 2025-01-28 03:34:13 字数 1462 浏览 4 评论 0原文

在Ado Yaml管道中,我的一个工作中有一个内联脚本bash shell正在运行。我想在脚本稍后将计数器自动化用于循环的计数器。计数器表示一些JSON嵌入式密钥对的数量(这些键对的值保持变量)。像大多数ADO管道中一样,变量表(实际上是一个变量模板表,但这没关系)单独存储在管道上,并在YAML管道开始时被调用。

{
  "abc": {
    "models": {
      "model1": {
        "a": "x",
        "b": "z"
      },
      "model2": {
        "a": "x",
        "b": "z"
      },
      "model3": {
        "a": "x",
        "b": "z"
      }
    }
  }
}

此示例的期望结果将为3,但将来将添加更多模型。对于上下文,模型名称可能会更改 - 因此不可能做一些奇怪的事情,例如取最后一个钥匙对名称并拆分数字。最好是用狂欢来编写解决方案,因为我希望避免任何复杂性。

我能找到和测试的唯一相关内容是:

            modelCount5="$(jq 'abc.models | length' ${{ parameters.variableGroup }})"
            echo $modelCount5
            modelCount6="$(jq '.abc.models | length' ${{ parameters.variableGroup }})"
            echo $modelCount6
            modelCount7="$(jq '$(abc.models) | length' )"
            echo $modelCount7
            modelCount9="$(jq '$(abc.models) | length' ${{ parameters.variableGroup }})"
            echo $modelCount9

它们都导致了反复的错误或目录未发现错误。 例如:

jq: 1 compile error
or
jq: error: Could not open file CTS: No such file or directory

我已经检查了运行管道的代理,jq已预先安装。

回答

谢谢您的帮助。最后,我做到了:

   modelCount="$(jq '.variables.abc.models | length' $(Build.SourcesDirectory)/variables/templates/variables-sheet.jsonc)"   

...而且效果很好!这是正确配置我通往文件的路径的问题。

Within a ADO yaml pipeline one of my jobs has a inline script bash shell running. I want to automate a counter being used for a loop later in the script. The counter represents the number of some json embedded key pairs (the value of these key pairs hold variables). Like in most ADO pipelines the variable sheet (it's actually a variable template sheet but that doesn't matter) is stored separately to the pipeline and called on at the start of the yaml pipeline.

{
  "abc": {
    "models": {
      "model1": {
        "a": "x",
        "b": "z"
      },
      "model2": {
        "a": "x",
        "b": "z"
      },
      "model3": {
        "a": "x",
        "b": "z"
      }
    }
  }
}

The desired outcome of this example would be 3 but in the future more models will be added. For context the model names are subject to change - so it's not possible to do something odd like take the last key pair name and split off the number. It would be best if the solution is written in Bash as I would prefer to avoid any complexities.

The only related things I could find and test were:

            modelCount5="$(jq 'abc.models | length' ${{ parameters.variableGroup }})"
            echo $modelCount5
            modelCount6="$(jq '.abc.models | length' ${{ parameters.variableGroup }})"
            echo $modelCount6
            modelCount7="$(jq '$(abc.models) | length' )"
            echo $modelCount7
            modelCount9="$(jq '$(abc.models) | length' ${{ parameters.variableGroup }})"
            echo $modelCount9

They all resulted in complie errors or directory not found errors.
Eg:

jq: 1 compile error
or
jq: error: Could not open file CTS: No such file or directory

I have checked the agent running the pipeline and jq has been preinstalled.

Answer

Thankyou all for your help. In the end I did this:

   modelCount="$(jq '.variables.abc.models | length' $(Build.SourcesDirectory)/variables/templates/variables-sheet.jsonc)"   

...and it worked great! It was a matter of configuring my path to the file properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2025-02-04 03:34:13

您可以获取提取物,并将其存储在外壳变量中,并带有以下语句:

modelCount="$(jq '.variables.abc.models | length' path/to/your/file"

长度 在JQ中为您提供数组的长度。 并用父命令行使用的输出替换命令。

You can get the extract the count and store it in a shell variable with the following statement:

modelCount="$(jq '.variables.abc.models | length' path/to/your/file"

length in jq gives you the length of an array. $(…) is command substution and substitutes the command with its output to be used by the parent command line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文