简体   繁体   English

bash 试图将变量传递给 curl 命令

[英]bash trying to pass in a variable into curl command

Im trying to use variables inside a curl command and i either get one of the two following:我试图在 curl 命令中使用变量,我要么得到以下两个之一:

  1. test.sh line 32: unexpected EOF while looking for matching `"' test.sh 第 32 行:在寻找匹配`"'时出现意外的 EOF

    test.sh line 33: syntax error: unexpected end of file test.sh 第 33 行:语法错误:文件意外结束

  2. "$key"

My command is:我的命令是:

curl 'http://1.1.1.1:8080/v2-beta/projects/1a25/stack' -H 'content-type: application/json' -data-binary $'{"system":true,"type":"stack","name":"ecr","startOnCreate":true,"environment":{"key_id":"$key","access_key":"214356","aws_region":"ap-southeast-2","auto_create":"false","log_level":"INFO","registry_in_which_environment":"current","environment_api_endpoint":"12345.dkr.amazonaws.com","environment_api_access_key":"7AC2D3FE5A1C12345","environment_api_secret_key":"QMmwp7ebmzK1ZcUCjoRM12345"},"dockerCompose":"ecr-updater:\\n  environment:\\n    AWS_ACCESS_KEY_ID: ${aws_access_key_id}\\n    AWS_SECRET_ACCESS_KEY: ${aws_secret_access_key}\\n    AWS_REGION: ${aws_region}\\n    AUTO_CREATE: ${auto_create}\\n    LOG_LEVEL: ${log_level}\\n    {{- if eq .Values.registry_in_which_environment \\"other\\" }}\\n    CATTLE_URL: ${environment_api_endpoint}\\n    CATTLE_ACCESS_KEY: ${environment_api_access_key}\\n    CATTLE_SECRET_KEY: ${environment_api_secret_key}\\n    {{- end }}\\n  labels:\\n    io.rancher.container.pull_image: always\\n    {{- if eq .Values.registry_in_which_environment \\"current\\" }}\\n    io.rancher.container.create_agent: \'true\'\\n    io.rancher.container.agent.role: environment\\n    {{- end }}\\n  tty: true\\n  image: rancher/rancher-ecr-credentials:v2.0.1\\n  stdin_open: true\\n","rancherCompose":".catalog:\\n  name: \\"ECR Credential Updater\\"\\n  version: \\"v2.0.1\\"\\n  description: \\"Updates credentials for ECR in Rancher\\"\\n  minimum_rancher_version: \\"v1.6.13-rc1\\"\\n  questions:\\n    - variable: \\"aws_access_key_id\\"\\n      label: \\"AWS Access Key ID\\"\\n      description: \\"AWS API Access Key to use for obtaining ECR credentials. Not required if using IAM roles.\\"\\n      required: false\\n      type: \\"string\\"\\n    - variable: \\"aws_secret_access_key\\"\\n      label: \\"AWS Secret Access Key\\"\\n      description: \\"AWS API Secret Key to use for obtaining ECR credentials. Not required if using IAM roles.\\"\\n      required: false\\n      type: \\"string\\"\\n    - variable: \\"aws_region\\"\\n      label: \\"AWS Region\\"\\n      description: \\"AWS Region that hosts the ECR\\"\\n      default: us-east-1\\n      required: true\\n      type: \\"string\\"\\n    - variable: \\"auto_create\\"\\n      label: \\"Auto Create\\"\\n      description: \\"Automatically create registry in Rancher for corresponding ECR repo if it doesn\'t already exist\\"\\n      default: \\"false\\"\\n      required: true\\n      type: \\"string\\"\\n    - variable: \\"log_level\\"\\n      label: \\"Log Level\\"\\n      description: \\"Logging level to run service at\\"\\n      default: \\"INFO\\"\\n      required: true\\n      type: \\"string\\"\\n    - variable: \\"registry_in_which_environment\\"\\n      type: enum\\n      label: \\"Registry Environment\\"\\n      description: \\"Which environment is the AWS registry located?\\"\\n      default: current\\n      options:\\n        - current\\n        - other\\n    - variable: \\"environment_api_endpoint\\"\\n      label: \\"URL (Environment API Endpoint) of Registry to be Updated\\"\\n      description: \\"URL for where the registry is located. This is the endpoint, which can be found under the Advanced Options under API -> Keys, for an Environment API Key.\\"\\n      default: \\"\\"\\n      required: false\\n      type: \\"string\\"\\n    - variable: \\"environment_api_access_key\\"\\n      label: \\"Access Key of an Environment API Key\\"\\n      description: \\"This access key must be for the environment where the registry will need to be updated. This access key will be created in the same environment listed for the URL. This can be created in the Advanced Options under API.\\"\\n      default: \\"\\"\\n      required: false\\n      type: \\"string\\"\\n    - variable: \\"environment_api_secret_key\\"\\n      label: \\"Secret Key of an Environment API Key\\"\\n      description: \\"This secret key must be for the environment where the registry will need to be updated. This secret key will be created in the same environment listed for the URL. This can be created in the Advanced Options under API.\\"\\n      default: \\"\\"\\n      required: false\\n      type: \\"string\\"\\necr-updater:\\n  scale: 1\\n","externalId":"catalog://library:infra*ecr:1"}'

In the following curl you will see two working variables:在以下 curl 中,您将看到两个工作变量:

  1. $backup_hash without double quotes $backup_hash 不带双引号

  2. $index with double quotes inside a JSON $index 在 JSON 中带有双引号

curl -k -XPOST 'somesite/_snapshot/cs-automated/'"$backup_hash"'/_restore' -d '{"indices": "'"$index"'"}' -H 'Content-Type: application/json'

Use an editor with syntax highlight to ensure your code is not broken:使用带有语法高亮的编辑器来确保你的代码没有被破坏:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM