Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read each line from a file and use it as a variable in curl command

Tags:

bash

curl

wget

I have a file called list which has something like

1.html
2.html
3.html

I want to run a curl command to read each line from the file and use it as a variable for curl, e.g.:

curl "xyz.com/v1/{input from each line of file} 

Please help....

like image 898
Raj Avatar asked Mar 12 '23 09:03

Raj


1 Answers

Try:

xargs -I{} curl "xyz.com/v1/"{} <file

For each line in your file, this runs curl "xyz.com/v1/" followed by the contents of that line.

To illustrate what happens, without downloading anything, we can add an echo command:

$ xargs -I{} echo curl "xyz.com/v1/"{} <file
curl xyz.com/v1/1.html
curl xyz.com/v1/2.html
curl xyz.com/v1/3.html

Note that I left the base URL, xyz.com/v1/, in double-quotes as it was in the question. That means that it is subject to shell expansions. If you don't want that (and you likely don't), then use single-quotes:

xargs -I{} curl 'xyz.com/v1/'{} <file
like image 62
John1024 Avatar answered Mar 14 '23 02:03

John1024