用空格分隔的值;如何提供一个包含空格的值 [英] Space separated values; how to provide a value containing a space

查看:90
本文介绍了用空格分隔的值;如何提供一个包含空格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个bash脚本,以通过 Azure CLI .到目前为止,还不错,但是我在标记资源时遇到了问题.

I'm creating a bash script to provision multiple Azure resources via the Azure CLI. So far so good, however I'm having a problem tagging resources.

我的目标是将多个标签存储在一个变量中,并将该变量提供给脚本中几个 az 命令的--tags选项.但是,问题在于值中的空格将被解释​​为新键.

My goal is to store multiple tags in a variable and provide that variable to the --tags option of several az commands in the script. The problem however is that a space in the value will be interpreted as a new key.

例如,如果我们使用命令 az group update (它将更新资源组),则

If we take for example the command az group update (which will update a resource group) the docs state the following about the --tags option:

-标签以键[=值]"格式用空格分隔的标签.使用"清除现有标签.

--tags Space-separated tags in 'key[=value]' format. Use "" to clear existing tags.

当值(或键)包含空格时,必须将其括在引号中.因此,当我们将键值对直接提供给包含空格的值的命令时,如下面的示例所示,结果将如预期的那样:

When a value (or key) contains spaces it must be enclosed in quotes. So when we provide the key-value pairs directly to the command including a value with spaces, like in the following example, the result will be as expected:

az group update --tags owner="FirstName LastName" application=coolapp --name resource-group-name

结果将是两个标签已添加到资源组:

The result will be that two tags have been added to the resource group:

{
  "id": "/subscriptions/1e42c44c-bc55-4b8a-b35e-de1dfbcfe481/resourceGroups/resource-group-name",
  "location": "westeurope",
  "managedBy": null,
  "name": "resource-group-name",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": {
    "application": "coolapp",
    "owner": "FirstName LastName"
  }
}

但是,当我们将上一步中使用的相同值存储在变量中时,就会出现问题.

However, when we store the same value we used in the previous step in a variable the problem occurs.

tag='owner="FirstName LastName" application=coolapp'

我使用 echo $ tag 来验证该变量包含的值与我们在上一个示例中为--tags选项提供的值完全相同:

I use echo $tag to validate that the variable contains exactly the same value as we provided in the previous example to the --tags option:

owner="FirstName LastName" application=coolapp

但是当我们向命令的标签选项提供此标签变量时,如下一行所示:

But when we provide this tag variable to the tags option of the command as shown in the next line:

az group update --tags $tag --name resource-group-name

结果将是三个标签,而不是预期的两个:

The result will be three tags instead of the expected two:

{
  "id": "/subscriptions/1e42c44c-bc55-4b8a-b35e-de1dfbcfe481/resourceGroups/resource-group-name",
  "location": "westeurope",
  "managedBy": null,
  "name": "resource-group-name",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": {
    "LastName\"": "",
    "application": "coolapp",
    "owner": "\"FirstName"
  }
}

我已经尝试通过以下方式定义变量,但到目前为止还没有运气:

I've already tried defining the variable in the following ways, but no luck so far:

tag="owner=FirstName LastName application=coolapp"
tag=owner="Firstname Lastname" application=cool-name
tag='`owner="Firstname Lastname" application=cool-name`'

我什至尝试将变量定义为数组,并将其提供给命令,如下行所示,但这也没有提供正确的结果:

I even tried defining the variable as an array and providing it to the command as shown on the next line, but also that didn't provide the correct result:

tag=(owner="Firstname Lastname" application=cool-name)

az group update --tags ${tag[*]}--name resource-group-name

我也尝试过在命令中的变量周围加上引号,如@socowi所建议的那样,但这会导致以下错误结果,即一个标记而不是两个标记:

I also tried putting quotes around the variable in the command, as was suggested by @socowi, but this leads to the following incorrect result of one tag instead of two:

az group update --tags "$tag" --name resource-group-name

{
  "id": "/subscriptions/1e42c44c-bc55-4b8a-b35e-de1dfbcfe481/resourceGroups/resource-group-name",
  "location": "westeurope",
  "managedBy": null,
  "name": "resource-group-name",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": {
    "owner": "Firstname Lastname application=cool-name"
  }
}

有人知道如何解决这个问题吗?

Does anybody know how to solve this?

推荐答案

将标签定义为

tags=("owner=Firstname Lastname" "application=cool-name")

然后使用

--tags "${tags[@]}"

这篇关于用空格分隔的值;如何提供一个包含空格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆