Bash卷曲和网址中间的变量 [英] Bash curl and variable in the middle of the url

查看:78
本文介绍了Bash卷曲和网址中间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用curl读取某些数据。我基本上是从文件中读取关键字

I would need to read certain data using curl. I'm basically reading keywords from file

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

无论如何,我还没有找到形成卷曲URL的方法,因此它可以工作。我已经尝试过使用所有可能的单引号和双引号版本。我基本上已经尝试过:

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:

'...'"$line"'...'
"..."${line}"..."
'...'$line'...'

,依此类推。等等。只要命名即可,我很确定自己已经尝试过了。

and so on.. Just name it and I'm pretty sure that I've tried it.

在最佳情况下打印URL时,其格式为:

When I'm printing out the URL in the best case it will be formed as:

 /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!

推荐答案

这不是报价问题。问题是您的关键字文件是DOS格式的-也就是说,每行以回车符&结束。换行(\r\n),而不仅仅是换行(\n)。回车被读取到line变量中,并包含在URL中。赠品是,当您回显它时,它似乎会打印:

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:

/subscriptions?v=2&alt=jsoneeds/api/users/KEYWORD FROM FILE"

但这确实是在打印:

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.

那么您能怎么做呢?这是一种很简单的方法来减少行尾的cr:

So what can you do about it? Here's a fairly easy way to trim the cr at the end of the line:

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

这篇关于Bash卷曲和网址中间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆