Bash curl 和 url 中间的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8865241/
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
Bash curl and variable in the middle of the url
提问by Mare
I would need to read certain data using curl. I'm basically reading keywords from file
我需要使用 curl 读取某些数据。我基本上是从文件中读取关键字
while read line
do
curl 'https://gdata.youtube.com/feeds/api/users/'"${line}"'/subscriptions?v=2&alt=json' \
> '/home/user/archive/'"$line"
done < textfile.txt
Anyway I haven't found a way to form the url to curl so it would work. I've tried like every possible single and double quoted versions. I've tried basically:
无论如何,我还没有找到一种方法来形成 url 来卷曲,所以它会起作用。我已经尝试过所有可能的单引号和双引号版本。我基本上尝试过:
'...'"$line"'...'
"..."${line}"..."
'...'$line'...'
and so on.. Just name it and I'm pretty sure that I've tried it.
等等......只要命名它,我很确定我已经尝试过了。
When I'm printing out the URL in the best case it will be formed as:
当我在最好的情况下打印出 URL 时,它将形成为:
/subscriptions?v=2&alt=jsoneeds/api/users/KEYWORD FROM FILE
or something similar. If you know what could be the cause of this I would appreciate the information. Thanks!
或类似的东西。如果您知道这可能是什么原因,我将不胜感激。谢谢!
回答by Gordon Davisson
It's not a quoting issue. The problem is that your keyword file is in DOS format -- that is, each line ends with carriage return & linefeed (\r\n) rather than just linefeed (\n). The carriage return is getting read into the line variable, and included in the URL. The giveaway is that when you echo it, it appears to print:
这不是引用问题。问题在于您的关键字文件是 DOS 格式——也就是说,每一行都以回车符和换行符 (\r\n) 结尾,而不仅仅是换行符 (\n)。回车被读入行变量,并包含在 URL 中。赠品是,当您回显它时,它似乎打印:
/subscriptions?v=2&alt=jsoneeds/api/users/KEYWORD FROM FILE"
but it's really printing:
但它确实在打印:
https://gdata.youtube.com/feeds/api/users/KEYWORD FROM FILE
/subscriptions?v=2&alt=json
...with just a carriage return between them, so the second overwrites the first.
...它们之间只有一个回车,所以第二个会覆盖第一个。
So what can you do about it? Here's a fairly easy way to trim the cr at the end of the line:
所以你对此能做些什么?这是在行尾修剪 cr 的一种相当简单的方法:
cr=$'\r'
while read line
do
line="${line%$cr}"
curl "https://gdata.youtube.com/feeds/api/users/${line}/subscriptions?v=2&alt=json" \
> "/home/user/archive/$line"
done < textfile.txt
回答by Fred Foo
Your current version should work, I think. More elegant is to use a single pair of double quotes around the whole URL with the variable in ${}
:
我认为您当前的版本应该可以工作。更优雅的是在整个 URL 周围使用一对双引号,变量 in ${}
:
"https://gdata.youtube.com/feeds/api/users/${line}/subscriptions?v=2&alt=json"
回答by Mattias Ahnberg
Just use it like this, should be sufficient enough:
像这样使用它,应该足够了:
curl "https://gdata.youtube.com/feeds/api/users/${line}/subscriptions?v=2&alt=json" > "/home/user/archive/${line}"
If your shell gives you issues with &
just put \&
, but it works fine for me without it.
如果你的 shell 给你&
put问题\&
,但没有它它对我来说很好用。
回答by Jonathan Leffler
If the data from the file can contain spaces and you have no objection to spaces in the file name in the /home/user/archive
directory, then what you've got should be OK.
如果文件中的数据可以包含空格,并且您不反对/home/user/archive
目录中文件名中的空格,那么您得到的应该没问题。
Given the contents of the rest of the URL, you could even just write:
鉴于 URL 其余部分的内容,您甚至可以只写:
while read line
do
curl "https://gdata.youtube.com/feeds/api/users/${line}/subscriptions?v=2&alt=json" \
> "/home/user/archive/${line}"
done < textfile.txt
where strictly the ${line}
could be just $line
in both places. This works because the strings are fixed and don't contain shell metacharacters.
严格的地方${line}
可能只是$line
在两个地方。这是有效的,因为字符串是固定的并且不包含 shell 元字符。
Since you're code is close to this, but you claim that you're seeing the keywords from the file in the wrong place, maybe a little rewriting for ease of debugging is in order:
由于您的代码接近于此,但您声称您在错误的位置看到了文件中的关键字,因此为了便于调试可能需要进行一些重写:
while read line
do
url="https://gdata.youtube.com/feeds/api/users/${line}/subscriptions?v=2&alt=json"
file="/home/user/archive/${line}"
curl "$url" > "$file"
done < textfile.txt
Since the strings may end up containing spaces, it seems (do you need to expand spaces to +
in the URL?), the quotes around the variables are strongly recommended. You can now run the script with sh -x
(or add a line set -x
to the script) and see what the shell thinks it is doing as it is doing it.
由于字符串可能最终包含空格,似乎(您是否需要将空格扩展到+
URL 中?),强烈建议在变量周围使用引号。您现在可以使用sh -x
(或在脚本中添加一行set -x
)运行脚本并查看 shell 认为它正在执行的操作。