0

I run this command in a single bash file to post the data to a url and get the response

curl  -s -X  POST $url -d "username=username"

It outputs the response body. All good But I want to automate this task so I have saved many URLs in a file called urls.txt which looks like this:

https://url1.com
https://url2.com
and so on...

To loop all the lines(url's) in the file I run this script

while IFS="" read -r p || [ -n "$p" ]
do
  curl -s -X  POST $p -d "username=username" >> output.txt
done < urls.txt

I even try to run the loop with a hardcoded url, and it still doesn't work:

while IFS="" read -r p || [ -n "$p" ]
do
  curl -s -X  POST http://manual.url -d "username=username" >> output.txt
done < urls.txt

But I get no output saved or displayed. I don't know why is that. Any ideas? Can't I run curl in a snoop-a-loop?

1
  • What happens if you remove the -s ("silent") option and don't redirect into a file? Do you get the correct output in the terminal? What if you properly put the URL last on the command line and double quote it?
    – Kusalananda
    Commented Jan 7, 2019 at 13:10

1 Answer 1

1

You read the lines into a variable named p, but this is not used afterwards.

I would do it like this:

while IFS='' read -r url || [ -n "$url" ]; do
    echo "URL: >>$url<<"
    curl -s -X  POST $url -d "username=username" >> output.txt
done < urls.txt
4
  • I just edited it to $url for users to understand easier. But in fact in the code I have a problem its $p Still doesnt work Blank response even with your code Commented Jan 7, 2019 at 12:50
  • I edited to echo the read URL. You don't see that either?
    – Ralf
    Commented Jan 7, 2019 at 12:56
  • 1
    There is no issue with read -r url || [ -n "$url" ], this is a common way to read lines from files whose last line is not properly terminated by a newline.
    – Kusalananda
    Commented Jan 7, 2019 at 12:59
  • @Kusalananda Thanks! Learned something. Thought it is to filter out empty lines.
    – Ralf
    Commented Jan 7, 2019 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .