linux Shell脚本中的 curl 可能在标题部分失败

ao218c7q  于 2023-05-16  发布在  Linux
关注(0)|答案(1)|浏览(116)

我正在尝试运行一个脚本,它将获取bitwarden中的用户列表,并将其输出到一个名为bitmembers的文件。
下面是脚本,其中XXXX是我的API承载令牌

#!/bin/bash

CURL="/usr/bin/curl"
BITHTTP="https://api.bitwarden.com/public/members"
CURLARGS="-X GET"

header='-H "accept: text/plain" -H "Authorization: Bearer XXXX"'

$CURL $CURLARGS $BITHTTP $header > /tmp/bitmembers

下面是我在运行脚本时得到的错误:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: text
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: Bearer
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: XXXX

下面是curl命令和确认的工作:

curl -X GET "https://api.bitwarden.com/public/members" -H  "accept: text/plain" -H  "Authorization: Bearer XXXX"

它看起来像是在标题部分崩溃了。你能指出我漏掉了什么吗?

col17t5w

col17t5w1#

我认为问题在于curl选项没有设置在单独的变量中,所以下面的代码应该可以解决这个错误:

#!/bin/bash

CURL="/usr/bin/curl"
CURLARGS="-X GET"
BITHTTP="https://api.bitwarden.com/public/members"
header="-H"
accept="accept:text/plain"
auth="Authorization:Bearer XXXX"

$CURL $CURLARGS $BITHTTP $header "$accept" $header "$auth" > /tmp/bitmembers

This answer似乎是将变量作为选项发送到主curl命令的好方法。示例:

CURL_OPTIONS=( -X GET "$BITHTTP" -H "$accept" -H "$auth" )
$CURL "${CURL_OPTIONS[@]}"

相关问题