循环遍历行、 curl 值、存储值

a6b3iqyw  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(151)

我正在尝试读取一个文件,其中的每一行都是一个CVE ID。对于每个CVE,我想进行一次curl操作以获取其严重性,然后将结果存储在一个新的CSV文件中,格式为cve-id,cve-severity。
下面是我正在使用的脚本,它可以正确读取ID,但不能正确调用curl。当我运行它时,它只会为每个curl调用输出空值。
我试过用反勾号代替$(),但结果是一样的。我在这里做错了什么?

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

还尝试了这些变体,结果均相同(空):

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`

以下是CSV文件的示例:

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

gupuwyp21#

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

相关问题