外壳脚本错误:“循环”没有投掷预期的输出

发布于 2025-01-19 11:09:09 字数 1106 浏览 0 评论 0原文

我有JSON文件,可以从文件中提取颜色值。由于某种原因,它仅获取代码的一个块&其余的会引发错误。

摘要

#!/bin/bash

clear

echo "Add the figma json file path"
read path

figma_json="$(echo -e "${path}" | tr -d '[:space:]')"

echo "*****************************************"


color_values=$(cat $figma_json | jq -r '.color')
color_keys=$(cat $figma_json | jq -r '.color | keys' |sed 's,^ *,,; s, *$,,'| tr -s ' ' | tr ' ' '_')

echo $color_keys


for c_key in $color_keys
do

    echo "key string: $c_key"

    echo $color_values | jq ".$c_key.value"
    echo "*********************************************"

done

输出

trimmed string: "gray1",
{
  "description": "",
  "type": "color",
  "value": "#333333ff",
  "extensions": {
    "org.lukasoppermann.figmaDesignTokens": {
      "styleId": "S:0b49d19e868ec919fac01ec377bb989174094d7e,",
      "exportKey": "color"
    }
  }
}
null
*********************************************
trimmed string: "gray2" //Expected output
"#333333ff"
*********************************************

如果我们查看第二个输出,它将打印灰色的十六进制值,这是预期输出 请使用下面的链接获取JSON文件

I have json file which extract the color value from the file. For some reason, it only fetch only one block of code & for the rest it throws error.

snippet

#!/bin/bash

clear

echo "Add the figma json file path"
read path

figma_json="$(echo -e "${path}" | tr -d '[:space:]')"

echo "*****************************************"


color_values=$(cat $figma_json | jq -r '.color')
color_keys=$(cat $figma_json | jq -r '.color | keys' |sed 's,^ *,,; s, *$,,'| tr -s ' ' | tr ' ' '_')

echo $color_keys


for c_key in $color_keys
do

    echo "key string: $c_key"

    echo $color_values | jq ".$c_key.value"
    echo "*********************************************"

done

Output

trimmed string: "gray1",
{
  "description": "",
  "type": "color",
  "value": "#333333ff",
  "extensions": {
    "org.lukasoppermann.figmaDesignTokens": {
      "styleId": "S:0b49d19e868ec919fac01ec377bb989174094d7e,",
      "exportKey": "color"
    }
  }
}
null
*********************************************
trimmed string: "gray2" //Expected output
"#333333ff"
*********************************************

If we look at the second output it prints the hex value of gray2 which is the expected output
Please use the follow link to get the json file
link

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

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

发布评论

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

评论(1

初见你 2025-01-26 11:09:09

目前还不清楚您的目标是什么,但这是一种仅使用一次 jq 调用即可从 JSON 文件读取数据的方法,而且很可能不需要使用 sedtr。您可以根据自己的喜好轻松调整选择和格式。

jq -r '.color | to_entries[] | "\(.key): \(.value.value)"' "$figma_json"
gray1: #333333ff
gray2: #4f4f4fff
gray3: #828282ff
gray4: #bdbdbdff
gray5: #e0e0e0ff
gray6: #f2f2f2ff
red: #eb5757ff
orange: #f2994aff
yellow: #f2c94cff
green1: #219653ff
green2: #27ae60ff
green3: #6fcf97ff
blue1: #2f80edff
blue2: #2d9cdbff
blue3: #56ccf2ff
purple1: #9b51e0ff
purple2: #bb6bd9ff

演示

It's quite unclear what you are aiming at, but here's one way how you would read from a JSON file using just one call to jq, and most probably without the need to employ sed or tr. The selection as well as the formatting can easily be adjusted to your liking.

jq -r '.color | to_entries[] | "\(.key): \(.value.value)"' "$figma_json"
gray1: #333333ff
gray2: #4f4f4fff
gray3: #828282ff
gray4: #bdbdbdff
gray5: #e0e0e0ff
gray6: #f2f2f2ff
red: #eb5757ff
orange: #f2994aff
yellow: #f2c94cff
green1: #219653ff
green2: #27ae60ff
green3: #6fcf97ff
blue1: #2f80edff
blue2: #2d9cdbff
blue3: #56ccf2ff
purple1: #9b51e0ff
purple2: #bb6bd9ff

Demo

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