Home > OS >  Loop though lines, Curl for a value, Store the Value
Loop though lines, Curl for a value, Store the Value

Time:10-12

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?

#!/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

CodePudding user response:

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
  • Related