Script to get the HTTP status code of a list of urls?

前端 未结 8 865
无人及你
无人及你 2020-11-30 17:17

I have a list of URLS that I need to check, to see if they still work or not. I would like to write a bash script that does that for me.

I only need the returned HTT

8条回答
  •  [愿得一人]
    2020-11-30 17:49

    Curl has a specific option, --write-out, for this:

    $ curl -o /dev/null --silent --head --write-out '%{http_code}\n' 
    200
    
    • -o /dev/null throws away the usual output
    • --silent throws away the progress meter
    • --head makes a HEAD HTTP request, instead of GET
    • --write-out '%{http_code}\n' prints the required status code

    To wrap this up in a complete Bash script:

    #!/bin/bash
    while read LINE; do
      curl -o /dev/null --silent --head --write-out "%{http_code} $LINE\n" "$LINE"
    done < url-list.txt
    

    (Eagle-eyed readers will notice that this uses one curl process per URL, which imposes fork and TCP connection penalties. It would be faster if multiple URLs were combined in a single curl, but there isn't space to write out the monsterous repetition of options that curl requires to do this.)

提交回复
热议问题