I'm using cURL to check links for uptime in a bash script like this:
curl -Lo /dev/null --silent --head --write-out '%{http_code}' $link
Where link="http://deadlink/"
for example.
It returns 000 cos it's a dead link which is fine, but whenever I get a 000 response from cURL (since it could be for many reasons), I'd like to apply some logic to find out what's happening there. For instance, is it because connection refused, timeout, ssl failure, etc?
I assume the best way would some way to isolate the error code directly from cURL and apply tests to it using IF statements. That would be fine, so the closest I've got to extracting the error code from cURL by itself is this:
failState=$(curl -Ss $link; echo "error code is $?" )
echo $failState
Which nicely returns:
curl: (6) Could not resolve host: brokenlink
error code is 6
How do I get the "6" into a variable?