I am doing a shot here, but I think it bothers you that curl
sometimes returns more than one header information (hence more than one status code) when the initial request is redirected.
For instance:
[ me@hoe ]$ curl -sIL www.google.com | awk '/HTTP/{print $2}' 302 200
When you print this in a loop, it looks like the second status code has become part of the following URL.
If this is really your problem, then there are several ways to solve this problem, depending on what you are trying to achieve.
If you do not want to follow the redirects, just leave the -L
option in curl
statuscode=$(curl -sI $i | awk '/HTTP/{print $2}')
To take only the last status code, simply run the entire command to tail -n1
to take only the last.
statuscode=$(curl -sI $i | awk '/HTTP/{print $2}' | tail -n1)
To show all codes in order, replace all lines with spaces
statuscode=$(curl -sI $i | awk '/HTTP/{print $2}' | tr "\n" " ")
For example, using the third scenario:
[ me@home ]$ cat script.sh #!/bin/bash for URL in www.stackoverflow.com stackoverflow.com stackoverflow.com/xxx do statuscode=$(curl -siL $i | awk '/^HTTP/{print $2}' | tr '\n' ' ') echo -e "${URL}\t${statuscode}" done [ me@home ]$ ./script.sh www.stackoverflow.com 301 200 stackoverflow.com 200 stackoverflow.com/xxx 404
source share