【问题标题】:Expecting property name enclosed in double quotes - aws cli error期望用双引号括起来的属性名称 - aws cli 错误
【发布时间】:2020-09-07 20:28:35
【问题描述】:

我正在通过 teamcity 步骤在 bash 脚本中运行以下 awscli 命令

 #!/bin/bash 
  repo=%env.RepoName%
  echo "repo is ${repo}"
  tags=%env.Tags%
  echo "tags are ${tags}"
  aws ecr create-repository --repository-name ${repo} --tags '${tags}'

其中%env.RepoName%%env.Tags% 是teamcity 变量,其值分别为sample-repo[{"Key":"env","Value":"dev"},{"Key":"dept","Value":"finance"}]。但是,当 aws cli 命令运行时,会出现以下错误

Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

但是,如果我使用单引号将%env.Tags% 的值作为'[{"Key":"env","Value":"dev"},{"Key":"dept","Value":"finance"}]' 传递,并从aws cli 命令的tags 属性中排除'',它运行没有问题。

我缺少什么,有推荐的方法来解决这个问题吗?

【问题讨论】:

  • 单引号抑制扩展,因此您实际上是在发送 ${tags} 而不是其内容。
  • @Aserre 和本杰明谢谢。上面的链接解释了为什么这不起作用。但我仍然不确定如何解决我的特定问题。请问您有什么想法吗?
  • 换句话说,有没有办法可以在传递输入时排除 ' ' 并仍然使 aws-cli 命令在 bash 脚本中工作?

标签: bash amazon-web-services aws-cli amazon-ecr


【解决方案1】:

一种方法是通过使用\ 转义双引号来保留双引号。您可能需要写 sed 来执行此操作:

tags="[{\"Key\":\"env\",\"Value\":\"dev\"},\"Key\":\"dept\",\"Value\":\"finance\"}]"

完成后,使用echo 验证命令以检查引号是否保留。

echo aws ecr create-repository --repository-name ${repo} --tags "${tags}"                         
aws ecr create-repository --repository-name sample-repo --tags [{"Key":"env","Value":"dev"},{"Key":"dept","Value":"finance"}]

另一种方法是使用简写语法。如果可以单独获取标签键/值,这种语法看起来更简洁。

aws ecr create-repository --repository-name ${repo} --tags Key=env,Value=dev Key=dept,Value=finance

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多