bash script grep using variable fails to find result that actually does exist

后端 未结 2 1645
谎友^
谎友^ 2021-01-29 05:01

I have a bash script that iterates over a list of links, curl\'s down an html page per link, greps for a particular string format (syntax is: CVE-####-####), removes the surroun

相关标签:
2条回答
  • 2021-01-29 05:07

    HTML files can contain carriage returns at the ends of lines, you need to filter those out.

    curl -s "$link" | sed -n '/CVE-/s/<[^>]*>//gp' | tr -d '\r' | while read cve; do
    

    Notice that there's no need to use grep, you can use a regular expression filter in the sed command. (You can also use the tr command in sed to remove characters, but doing this for \r is cumbersome, so I piped to tr instead).

    0 讨论(0)
  • 2021-01-29 05:31

    It should look like this:

    # First: Care about quoting your variables!
    
    # Use read to read the file line by line
    while read -r link ; do
        # No grep required. sed can do that.
        curl -s "$link" | sed -n '/CVE-/s/<[^>]*>//gp' | while read -r cve; do
            echo "$cve"
            # grep -F searches for fixed strings instead of patterns
            grep -F "$cve" ./changelog.txt
        done
    done < links.txt
    
    0 讨论(0)
提交回复
热议问题