【发布时间】:2015-04-02 05:49:00
【问题描述】:
我正在使用 bash 脚本来动态创建 EC2 CLI 请求。执行 bash 脚本时,AWS CLI 返回Error parsing parameter '--launch-specification': Invalid JSON:,但如果我复制 CLI 字符串并直接通过 CLI 提交,则 CLI 命令可以正常工作。
bash 脚本是否会生成一些导致 CLI 请求失败的代码字符,而这些字符在我在终端中使用复制/粘贴时不存在?
BASH 脚本代码
CMD01=("aws --profile ${myProf} --region ${myRegion} ec2 request-spot-instances --spot-price ${PRICE} --instance-count ${6} --type \"one-time\" --launch-specification \"{\\\"ImageId\\\":\\\"${1}\\\",\\\"KeyName\\\":\\\"${2}\\\",\\\"InstanceType\\\":\\\"${!5}\\\",\\\"IamInstanceProfile\\\":{\\\"Arn\\\":\\\"${16}\\\"},\\\"Placement\\\":{\\\"AvailabilityZone\\\":\\\"${18}\\\",\\\"GroupName\\\":\\\"${11}\\\"},\\\"NetworkInterfaces\\\":[{\\\"DeviceIndex\\\":0,\\\"SubnetId\\\":\\\"${4}\\\",\\\"AssociatePublicIpAddress\\\":${17}}],\\\"UserData\\\":\\\"string\\\"}\" --dry-run")
回响通过
echo "$CMD01"
aws --profile myProfile --region eu-west-1 ec2 request-spot-instances --spot-price 0.004 --instance-count 1 --type "one-time" --launch-specification "{\ "ImageId\":\"ami-9c7ad8eb\",\"KeyName\":\"myKey\",\"InstanceType\":\"t1.micro\",\"IamIns tanceProfile\":{\"Arn \":\"arn:aws:iam::000000000000:instance-profile/myprofile\"},\"Placement\":{\"AvailabilityZone\":\"eu-west-1c\",\"GroupName\ ":\"myGroup\"},\"NetworkInterfaces\":[{\"DeviceIndex\":0,\"SubnetId\":\"subnet-xxxyyy\",\"AssociatePublicIpAddress\":true}],\ "UserData\":\"string\"}" --dry-run
通过${CMD01[@]} > $logFile执行
产生错误
解析参数“--launch-specification”时出错:JSON 无效: "{\"ImageId\":\"ami-9c7ad8eb\",\"KeyName\":\"myKey\",\"InstanceType\":\"t1.micro\",\"IamInstanceProfile\":{\ "Arn\":\"arn:aws:iam::000000000000:instance-profile/myprofile\"},\"Placement\":{\"AvailabilityZone\":\"eu-west-1c\",\" GroupName\":\"myGroup\"},\"NetworkInterfaces\":[{\"DeviceIndex\":0,\"SubnetId\":\"subnet-xxxyyy\",\"AssociatePublicIpAddress\":true}] ,\"用户数据\":\"字符串\"}"
现在,如果我从终端获取较早的 echo echo "$CMD01" 并进行简单的复制/粘贴,CLI 输出
调用 RequestSpotInstances 操作时发生客户端错误 (DryRunOperation):请求会成功,但设置了 DryRun 标志。
所以看起来 JSON 是有效的,但是当从 bash 脚本执行时它是无效的。我做错了什么?
【问题讨论】:
标签: json bash amazon-web-services command-line-interface aws-cli