Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash curl and variable in the middle of the url

Tags:

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 

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.

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!

like image 401
Mare Avatar asked Jan 14 '12 20:01

Mare


People also ask

Does curl use environment variables?

curl checks for the existence of specially named environment variables before it runs to see if a proxy is requested to get used. While the above example shows HTTP, you can, of course, also set ftp_proxy, https_proxy, and so on.

How do I declare a variable in bash?

A variable in bash is created by assigning a value to its reference. Although the built-in declare statement does not need to be used to explicitly declare a variable in bash, the command is often employed for more advanced variable management tasks.


1 Answers

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" 

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=$'\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 
like image 120
Gordon Davisson Avatar answered Oct 04 '22 00:10

Gordon Davisson