使用bash脚本

时间:2016-01-27 09:15:59

标签: bash curl sh

我想用curl脚本创建#!/bin/sh,让我头疼的部分是:

STR="-XGET -v -H 'Authorization: Bearer "$TOKEN"' https://my_endpoint/sth?"
echo $STR

CON="$(curl $STR)"

那里的回声是:

-XGET -v -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWljaGVyMjEwLmNvbSIsImlhdCI6IjE0NTM4ODU3ODcifQ.ADLcrV4yph6owwgMLak2RsnC95WK17ULflCisvNWBkeA93G4WUQ-BMrjnhQeuIgSfxSYnAmgmI36ggc2PytWhkqk8lIYrMJTH80tggBYCnnuA2lM26IZ2ViUMK1cj-BH3-dh4HmqSm_hozAFnVqGQi9P5J4CBz8eCf_mKc3iq-7EnXRikTkgakF69-jPfFA_9yO26JzZeDpymowa-LRPafWPtYinzmkaUQ2SHjUdWtGmELAyzkGUOOXrZ8TgvV9Yeb-OnEoY54GRSlb4ogVzAwWCJ2Y6vxmvNpAN5wiUZMylqTGhnqFr9MOp4JId1RavjwT7STRp9bCHBxD55CtYoEQ-oSpDv6WkgB07CtCRi0Spx9ErVsaB0Xf1mH9XKAVjOQ_dNNpKTxlqIXMbbosxEhjYE9K6Z30c3uWhCgccNdEEHxPhi7d2bRO3M_3fJPKsYWWk5DXhxmkFpJ4fLf05JO31FFIoj8q7H3c5NEvXVk_keS-jPY5iP5xRY1dv9P8bWPEwFk1-qQrXZ1mMNiLDLxdB9cXE9Tm6Eo4Rxo71H2o4Z1DHmnVHHctsATzywsJIe3o8Ym5o0OsmS3WH3EJ-IS572lFv-ZQSkt3fq927JlvWotd9HHMT2MOPf8Zg5cdNd-hclZUfj7qOi-a0Afbn2cT3FccBXJ8l4' https://my_endpoint/sth?

当我从终端复制并将其粘贴到curl时,它可以工作,但在详细日志中我发现:

-XGET -v -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWljaGVyMjEwLmNvbSIsImlhdCI6IjE0NTM4ODU3ODcifQ.ADLcrV4yph6owwgMLak2RsnC95WK17ULflCisvNWBkeA93G4WUQ-BMrjnhQeuIgSfxSYnAmgmI36ggc2PytWhkqk8lIYrMJTH80tggBYCnnuA2lM26IZ2ViUMK1cj-BH3-dh4HmqSm_hozAFnVqGQi9P5J4CBz8eCf_mKc3iq-7EnXRikTkgakF69-jPfFA_9yO26JzZeDpymowa-LRPafWPtYinzmkaUQ2SHjUdWtGmELAyzkGUOOXrZ8TgvV9Yeb-OnEoY54GRSlb4ogVzAwWCJ2Y6vxmvNpAN5wiUZMylqTGhnqFr9MOp4JId1RavjwT7STRp9bCHBxD55CtYoEQ-oSpDv6WkgB07CtCRi0Spx9ErVsaB0Xf1mH9XKAVjOQ_dNNpKTxlqIXMbbosxEhjYE9K6Z30c3uWhCgccNdEEHxPhi7d2bRO3M_3fJPKsYWWk5DXhxmkFpJ4fLf05JO31FFIoj8q7H3c5NEvXVk_keS-jPY5iP5xRY1yirgabLv9P8bWPEwFk1-qQrXZ1mMNiLDLxdB9cXE9Tm6Eo4Rxo71H2o4Z1DHmnVHHctsATzywsJIe3o8Ym5o0OsmS3WH3EJ-IS572lFv-ZQSkt3fq927JlvWot5MN9HHMT2MOPf8Zg5c6udNd-hclZUfj7qOi-a0Afbn2cT3FccBXJ8l4' https://my_endpoint/sth?
* Rebuilt URL to: Bearer/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Could not resolve host: Bearer
* Closing connection 0
curl: (6) Could not resolve host: Bearer
* Rebuilt URL to: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODc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hozAFnVqGQi9P5J4CBz8eCf_mKc3iq-7EnXRikTkgakF69-jPfFA_9yO26JzZeDpymowa-LRPafWPtYinzmkaUQ2SHjUdWtGmELAyzkGUOOXrZ8TgvV9Yeb-OnEoY54GRSlb4ogVzAwWCJ2Y6vxmvNpAN5wiUZMylqTGhnqFr9MOp4JId1RavjwT7STRp9bCHBxD55CtYoEQ-oSpDv6WkgB07CtCRi0Spx9ErVsaB0Xf1mH9XKAVjOQ_dNNpKTxlqIXMbbosxEhjYE9K6Z30c3uWhCgccNdEEHxPhi7d2bRO3M_3fJPKsYWWk5DXhxmkFpJ4fLf05JO31FFIoj8q7H3c5NEvXVk_keS-jPY5iP5xRY1yirgabLv9P8bWPEwFk1-qQrXZ1mMNiLDLxdB9cXE9Tm6Eo4Rxo71H2o4Z1DHmnVHHctsATzywsJIe3o8Ym5o0OsmS3WH3EJ-IS572lFv-ZQSkt3fq927JlvWot5MN9HHMT2MOPf8Zg5c6udNd-hclZUfj7qOi-a0Afbn2cT3FccBXJ8l4'
* Closing connection 1
curl: (6) Could not resolve host: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJleHAiOjE0NTM5NzIxODcsInVzZXJuYW1lIjoid2luZ3UtY2xpZW50LWFkbWluQHNwZWljaGVyMjEwLmNvbSIsImlhdCI6IjE0NTM4ODU3ODcifQ.ADLcrV4yph6owwgMLak2RsnC95WK17ULflCisvNWBkeA93G4WUQ-BMrjnhQeuIgSfxSYnAmgmI36ggc
*   Trying 54.228.198.226...
* Connected to my_endpoint (54.228.198.226) port 443 (#2)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.herokuapp.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET /api/sth? HTTP/1.1
> Host: my_endpoint/sth?
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Connection: keep-alive
< Server: nginx/1.8.0
< Content-Type: application/json
< Transfer-Encoding: chunked
< X-Powered-By: PHP/5.6.17
< Cache-Control: no-cache
< Allow: GET
< Date: Wed, 27 Jan 2016 09:09:49 GMT
< Via: 1.1 vegur
<
{ [59 bytes data]
100    48    0    48    0     0     41      0 --:--:--  0:00:01 --:--:--     0
* Connection #2 to host my_endpoint left intact

该标题有什么问题?

2 个答案:

答案 0 :(得分:1)

使用数组存储命令参数。

curl_options=(-XGET -v -H "Authorization: Bearer $TOKEN" "https://my_endpoint/sth?")
content=$(curl "${curl_options[@]}")

答案 1 :(得分:0)

您可以使用

eval curl $STR "https://my_endpoint" > 'output_file'