【问题标题】:Variable in command substitution is expanded into multiple arguments命令替换中的变量扩展为多个参数
【发布时间】:2021-11-10 22:09:20
【问题描述】:

我尝试在 bash 脚本中创建一个方法,该方法应该能够使用可变数量的标头执行 curl 操作,但是我似乎陷入了 curl 命令将标头参数视为多个参数的问题中它们包含空格。

当我在 bash 中运行以下行时,我得到 201 响应:

response=$($executable -X POST localhost:9200/index-template/globalmetadata --write-out '%{http_code}' --silent --output /dev/null --verbose --data "@${full_path}" -H "Content-Type: application/json" )

如果我运行以下命令:

#!/bin/bash

submit_request () {
  full_path=/home/mat/globalmetadata.json

  header_option=""
  header_options=""
  for header in "${@:1}"; do # looping over the  elements of the $@ array ($1, $2...)
    header_option=$(printf " -H %s" "$header")
    header_options=$(printf '%s%s' "$header_options" "$header_option")
  done

  echo Headers: $header_options

  executable=curl

  #response=$($executable -X POST localhost:9200/index-template/globalmetadata --write-out '%{http_code}' --silent --output /dev/null --verbose --data "@${full_path}" -H "Content-Type: application/json" )
  response=$($executable -X POST localhost:9200/index-template/globalmetadata --write-out '%{http_code}' --silent --output /dev/null --verbose --data "@${full_path}" $header_option )

  echo $response

}

submit_request "\"Content-Type: application/json\""

我得到这个输出:

Headers: -H "Content-Type: application/json"
======= 3
*   Trying 127.0.0.1:9200...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9200 (#0)
> POST /index-template/globalmetadata HTTP/1.1
> Host: localhost:9200
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Length: 3232
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 100 Continue
} [3232 bytes data]
* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 406 Not Acceptable
< X-elastic-product: Elasticsearch
< content-type: application/json; charset=UTF-8
< content-length: 97
< 
{ [97 bytes data]
* Connection #0 to host localhost left intact
* Could not resolve host: application
* Closing connection 1
406000

我注意到的是,即使标题是 -H "Content-Type: application/json,curl 也会显示 Could not resolve host: application。我怀疑由于Content-Type:application/json 之间的空格,它会将参数分成两部分。

我尝试在各种组合中混合和匹配引号和双引号,但没有任何效果。

【问题讨论】:

标签: bash shell command-substitution


【解决方案1】:

@GordonDavisson 是对的,您必须将您的 header_options 放入一个数组中。 请注意,当数组"${header_options[@]}" 为空时使用它会导致curl 命令中的参数为空,但是您可以通过将整个命令存储在另一个数组中来解决这个问题。 s>

submit_request () {

    local -a header_options
    for arg; do header_options+=(-H "$arg"); done

    local -a curl_command=( \
        curl \
        -X POST \
        localhost:9200/index-template/globalmetadata \
        --write-out '%{http_code}' \
        --silent \
        --output /dev/null \
        --verbose \
        --data '@/home/mat/globalmetadata.json' \
        "${header_options[@]}" \
    )

    local response=$( "${curl_command[@]}" )

    printf '%s\n' "$response"
}

【讨论】:

  • 我认为没有必要对空的header_options 数组采取预防措施。空数组扩展为空。
  • 你说得对,我被 printf '%q\n' "$@" 的参数测试误导了...
猜你喜欢
  • 2015-10-08
  • 2011-10-17
  • 2021-09-15
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2014-01-28
  • 2013-04-25
  • 1970-01-01
相关资源
最近更新 更多