Shell Script error: "For loop" is not throwing the expected output

Shell Script error: "For loop" is not throwing the expected output

我有 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"
*********************************************

如果我们查看第二个输出,它会打印出预期输出的 gray2 的十六进制值 请按照link获取json文件 link

目前还不清楚您的目标是什么,但这是一种方法,您只需调用一次 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

Demo