0

我有一个如下所示的 bash 脚本,并正确地将标签传递给包含空格的 az cli。

RESOURCE_GROUP_NAME=$1
LOCATION=$2
TAGS_INPUT_ARGUMENT=$3 # This TAGS_INPUT_ARGUMENT needs to finally look like the TAGS below.

echo 'TAGS_INPUT_ARGUMENT:' $TAGS_INPUT_ARGUMENT

# HARD CODED TAGS that Work
TAGS=("owner=Firstname Lastname" "application=cool-name")
echo 'TAGS:' "${TAGS[@]}"

az group create \
--name $RESOURCE_GROUP_NAME \
--location $LOCATION \
--tags "${TAGS[@]}"

我在将 TAGS 传递到脚本时遇到问题。
IE

export TAGS='["owner=Firstname Lastname","application=cool-name"]'
bash ./entrypoint.sh rg-lionking eastus2  "${TAGS}"    

输出:

TAGS_INPUT_ARGUMENT: ["owner=Firstname Lastname","application=cool-name"]
TAGS: owner=Firstname Lastname application=cool-name
{
  "id": "/subscriptions/**REDACTED**/resourceGroups/rg-lionking",
  "location": "eastus2",
  "managedBy": null,
  "name": "rg-lionking",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": {
    "application": "cool-name",
    "owner": "Firstname Lastname"
  },
  "type": "Microsoft.Resources/resourceGroups"
}

鉴于我可以使 TAGS 处于良好状态,我需要做什么才能以 json 格式传递相同的内容并将其作为 az group create cli 的最终参数工作?

我的 jp 知识几乎为零,所以我猜答案就在某个地方。

4

1 回答 1

0

在命令行上扩展数组的等价物是传递多个单独的参数

./entrypoint rg-lionking eastus2 "owner=Firstname Lastname" "application=cool-name"

...然后,在entrypoint

#!/usr/bin/env bash
resource_group_name=$1; shift
location=$1; shift
tags=( "$@" )

az group create \
  --name "$resource_group_name" \
  --location "$location" \
  --tags "${tags[@]}"
于 2020-04-21T19:16:30.067 回答