Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through CURL results

Tags:

bash

curl

I have the following script:

#!/bin/bash
for line in $('curl -s https://scans.io/data/rapid7/sonar.http/20141209-http.gz | zcat | head -n 1000 | dap json + select vhost + lines');
do
    echo "$line\n"
done

For which I am trying to achieve the following:

  1. Loop through a dynamically growing list of results from the scans.io curl
  2. Output the line which I then propose to pass to PHP to store and process

However I'm getting syntax error near unexpected token$'\r''` but am not a BASH expert to know what I need to tweak.

like image 567
Antony Avatar asked Apr 20 '26 13:04

Antony


1 Answers

Use Process-Substitution, with a while-loop, see why using for-loop for command output parsing is bad. The unsetting of IFS and read with the -r flag does not allow backslashes to escape any characters and treat strings "as-is".

#!/bin/bash

while IFS= read -r line
do
    printf '%s\n' "$line"
done < <(curl -s https://scans.io/data/rapid7/sonar.http/20141209-http.gz | zcat | head -n 1000 | dap json + select vhost + lines)
like image 173
Inian Avatar answered Apr 23 '26 01:04

Inian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!