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:

and so on...

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

while IFS="" read -r p || [ -n "$p" ]
  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" ]
  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?

  • 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 Jan 7 '19 at 13:10

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
  • 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 – Born vs. Me Jan 7 '19 at 12:50
  • I edited to echo the read URL. You don't see that either? – Ralf Jan 7 '19 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 Jan 7 '19 at 12:59
  • @Kusalananda Thanks! Learned something. Thought it is to filter out empty lines. – Ralf Jan 7 '19 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.