Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop though lines, Curl for a value, Store the Value

I’m trying to read a file, each line of which is a CVE ID. For each CVE, I want to make a curl to get its severity and then store that result in a new CSV file with the format cve-id,cve-severity.

Below is the script I’m using, which reads the IDs correctly, but doesn’t make the curl call correctly. When I run this, it just outputs empty values for each curl call.

I’ve tried using back ticks instead of the $(), but same result. What am I doing wrong here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/bash
 
filename="cves.csv"
 
while read line
do

    echo "$line"
    cve_result=$(curl -s "https://cve.circl.lu/api/cve/${line}")
    echo "$cve_result"

done < $filename

Also tried these variations, all with same (empty) result:

cve_result=$(curl -s "https://cve.circl.lu/api/cve/${line}")
cve_result=`curl -s "https://cve.circl.lu/api/cve/${line}"`
cve_result=$(curl -s "https://cve.circl.lu/api/cve/$line")
cve_result=`curl -s "https://cve.circl.lu/api/cve/$line"`
cve_result=$(curl -s https://cve.circl.lu/api/cve/$line)
cve_result=`curl -s https://cve.circl.lu/api/cve/$line`

Here is a sample of the CSV file:

CVE-2014-0114
CVE-2014-9970
CVE-2015-1832
CVE-2015-2080
CVE-2015-7521

>Solution :

Your code works for me (ie, each curl call pulls down a bunch of data).

If I convert my (linux) file to contain windows/dos line endings (\r\n) then the curl calls don’t generate anything.

At this point I’m guessing your input file has windows/dos line endings (you can verify by running head -2 cves.csv | od -c and you should see the sequence \r \n at the end of each line).

Assuming this is your issue then you need to remove the \r characters; a couple options:

  • dos2unix cves.csv – only have to run once as this will update the file
  • curl ... ${line//$'\r'/}" – use parameter substitution to strip out the \r
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading