bash URL 中的空格导致 wget 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15055981/
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
Whitespaces in URL causing wget to fail
提问by Sriram
I have a URL like so:http://localhost:8999/createAudio?method=createAudio&sessionId=1234&text=${line}&voice=Paul&encoder=MP3";
Where ${line}is "This is such a nice day."
I am using wgetto capture the output of the above http request like so:cURL="http://localhost:8999/createAudio?method=createAudio&sessionId=1234&text=${line}&voice=Sangeeta&encoder=MP3";
wget -O test.html ${cURL}
我有一个这样的 URL:“这是多么美好的一天”http://localhost:8999/createAudio?method=createAudio&sessionId=1234&text=${line}&voice=Paul&encoder=MP3";
在哪里${line}。
我正在使用wget捕获上述 http 请求的输出,如下所示:cURL="http://localhost:8999/createAudio?method=createAudio&sessionId=1234&text=${line}&voice=Sangeeta&encoder=MP3";
wget -O test.html ${cURL}
My problem is that the http request cuts at the first instance of a white space. So the http request that would be issued would go like:http://localhost:8999/createAudio?method=createAudio&sessionId=1234&text=This
我的问题是 http 请求在空白的第一个实例处被切断。因此,将发出的 http 请求将如下所示:http://localhost:8999/createAudio?method=createAudio&sessionId=1234&text=This
How do I ensure that the entire ${line}gets used in the http request?
如何确保${line}在 http 请求中使用整个内容?
回答by John Flatness
One answer would be to simply URL-encode your spaces as %20, getting rid of any literal spaces.
一个答案是简单地将您的空格 URL 编码为%20,去掉任何文字空格。
However, wgetwill do this for you. Your problem here is that you've correctly quoted when you're initializing the cURLvariable, but not when you're actually usingit in the curl command line. You need to quote there also, or else bash will interpret any spaces in that variable as argument separators:
但是,wget会为你做这件事。您的问题是您在初始化cURL变量时正确引用了引号,但在 curl 命令行中实际使用它时却没有正确引用。您还需要在那里引用,否则 bash 会将该变量中的任何空格解释为参数分隔符:
wget -O test.html "$cURL"

