在bash脚本中使用curl并获取curl:(3)在URL

时间:2016-01-26 16:33:13

标签: linux bash curl awk

所以我有一个非常简单的bash脚本,它可以卷入一个auth服务器来获取头文件。标题url被写入var,然后在下一个curl调用中使用。在第一个curl调用中使用var set时,我得到" curl:(3)在URL"中找到非法字符。我能够回显var并且所有看起来都很好,我甚至可以重置var(在我下面的示例中)并且它可以工作。

Bash脚本

URL=$(curl -i -X GET -H "X-Auth-User: MyUserna,e" -H "X-Auth-Key: MyAPIKey" "https://urlToAuthServer.tld/auth/v1.0/" | grep "X-Storage-Url:" | awk '{print $2}')

curl -X GET -H "X-Auth-Token: MyAuthTok" "${URL}/folder/myfile.txt" -o ./myfile.txt

运行上面的示例时,我得到:

curl: (3) Illegal characters found in URL

网址var看起来像这样(没有非法字符)

https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534

当我在终端中执行此操作时,它可以工作:

  

$ URL = $(curl -i -X GET -H" X-Auth-User:MyUserna,e" -H" X-Auth-Key:MyAPIKey"&# 34; https://urlToAuthServer.tld/auth/v1.0/" | grep" X-Storage-Url:" | awk' {print $ 2}')

     

$ echo $ URL

     

https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534

现在我复制并粘贴字符串并将其重新分配到URL(就像在终端中一样):

>$ URL="https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534"
>$ curl -X GET -H "X-Auth-Token: MyAuthTok" "${URL}/folder/myfile.txt" -o ./myfile.txt

有效。

那么为什么我会得到"卷曲:(3)在URL"中找到非法字符。第一个例子中的错误?

更新 我跑了这个: printf %s "$URL" | xxd

这是输出(已经改变了你的想法)

0000000: 6874 7470 733a 2f2f 6461 6c30 352e 6f62  https://server.ob
0000010: 6a65 6374 7374 6f72 6167 652e 736f 6674  jectstorage.lite
0000020: 6c61 7965 722e 6e65 742f 7631 2f41 5554  sabers.com/v1/AUT
0000030: 485f 6665 3235 3339 3434 2d38 6433 322d  H_aE2563981-7d32-
0000040: 3432 3138 2d61 6566 632d 6665 6638 3465  4201-bdoi-fef94a
0000050: 6166 3331 6232 0d                        ag11c8.

3 个答案:

答案 0 :(得分:87)

$ URL末尾包含\r(CR)(0d)。用

删除它
URL=${URL%$'\r'}

curl一起使用之前。

答案 1 :(得分:1)

我遇到了同样的问题,但这是由于我通过Windows创建脚本而导致的,这意味着行尾有不必要的\ r。

一个简单的dos到unix的转换将其修复。

dos2unix <scriptname>

答案 2 :(得分:1)

取消培训\ r

tr -d '\r' < test.sh > testWithoutR.sh