You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shell脚本中无法动态调用YAML解析生成的嵌套变量问题

问题描述

我们使用了一套可按YAML键值对生成变量的方案解析YAML文件,符合预期需求。

我们尝试的脚本如下:

#!/bin/bash
source parse_yaml.sh
eval $(parse_yaml sample2.yaml policy)

echo ".............Eval Result..............................."
for f in $policy_ ; do eval echo \$f \$${f}_ ; done
echo "............Eval Result................................"

for f in $policy_ ; do
  if [[ $(eval echo \$${f}_name) == "ipfilter" ]]; then
    echo " given policy name is ipfilter "
    for g in $(eval echo \$${f}_session_); do
      if [[ $(eval echo \$${g}) == "inbound" ]]; then
      echo "add the add above Ipfilter string to the inbound session of xml"
      fi
      if [[ $(eval echo \$${g}) == "outbound" ]]; then
      echo "add the add above Ipfilter string to the outbound session of xml"
      fi
      if [[ $(eval echo \$${g}) == "backend" ]]; then
      echo "add the add above Ipfilter string to the backend session of xml"
      fi
    done
    for h in $(eval echo \$${f}_api_); do
      echo "decided the scope of Ipfilter policy as $(eval echo \$${h})"
    done
    for i in $(eval echo \$${f}_operations_); do
      echo "decided the scope of Ipfilter policy as $(eval echo \$${i})"
    done
  fi
done

场景与需求

我们的场景中,Policy列表数量不固定,每个Policy可包含多个session和operation列表。需求是根据每个Policy的属性生成自定义策略文件并应用到对应范围。

Policy YAML示例

- name: policy1
  session:
     - inbound
     - backend
  scope: api
  apiname:
  customvalue1: xxxxx
  customvalue2: xxxxx


- name: policyB
  scope: operation
  operation:
      - operation1
      - operation2
  session:
     - inbound
     - backend
  customvalue3: xxxxx
  customvalue4: xxxxx

# 更多Policy...

当前问题

在此场景下,我们无法动态循环Policy的session、operation等属性来添加对应条件逻辑。


内容的提问来源于stack exchange,提问作者Vowneee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 16:40:24