bash curl: (3) 在 URL 中发现非法字符:${...%?} 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43609101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:04:12  来源:igfitidea点击:

curl: (3) Illegal characters found in URL : ${...%?} doesn't work

bashcurl

提问by Hélo?se Chauvel

I've been looking for a solution to my problem all the morning, especially in the 4 posts in https://stackoverflow.comhaving the same error name in their title but the solutions don't work for me.

我整个上午都在寻找解决我的问题的方法,尤其是在https://stackoverflow.com中标题中具有相同错误名称的 4 个帖子中,但这些解决方案对我不起作用。

I want to do several simple cURL requests put together in a Bash script. The request at the end of the file always works, whatever request it is. However the requests before return an error:

我想在 Bash 脚本中执行几个简单的 cURL 请求。文件末尾的请求始终有效,无论它是什么请求。但是之前的请求返回错误:

curl: (3) Illegal characters found in URL

curl: (3) 在 URL 中发现非法字符

I am pretty sure that it has something to do with the carriage return in my file. But I don't know how to deal with it. As I show in the picture below I tried to use ${url1%?}. I also tried ${url1%$'\r'}, but it doesn't change anything.

我很确定它与我的文件中的回车有关。但我不知道如何处理。如下图所示,我尝试使用${url1%?}. 我也试过${url1%$'\r'},但它没有改变任何东西。

Screenshot of file + results in terminal:

终端中文件 + 结果的屏幕截图:

screenshot of file + results in terminal

文件截图+终端结果

Any ideas?

有任何想法吗?

回答by Socowi

If your lines end with \r, stripping away the \rfrom the $urlwon't work, because the line

如果您的行以 结尾\r\r则从 中剥离$url将不起作用,因为该行

curl -o NUL "{url1%?}

also ends with a \r, which is appended to the url argument again.

也以 a 结尾\r,它再次附加到 url 参数。

Comment out the \r, that is

注释掉\r,即

url1="www.domain.tld/file"
curl -o NUL "${url1%?}" #

or

或者

url1="www.domain.tld/file" #
curl -o NUL "$url1" #

or convert the file before executing it

或在执行之前转换文件

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