How do I get (only) the http status of a site in a shell script?

   -s/--silent
          Silent or quiet mode. Don't show progress meter 
          or error messages.  Makes Curl mute.

So your res should look like

res=`curl -s -I $1 | grep HTTP/1.1 | awk {'print $2'}`

Result is Error 301 on google.com, for example.


you want

...
res=$( curl -w %{http_code} -s --output /dev/null $1)
...

That will give you the HTTP_STATUS code as the only output.


I'd do it like this to ensure i get redirected to the final host and get the response from it:

res=($(curl -sLI "${1}" | grep '^HTTP/1' | tail -1))
((res[1] == 200)) || echo ${res[2]}

Tags:

Curl

Output

Cron