curl -I
will always return 0
, if it managed to produce an output with the HEAD. You have two alternatives.
The first is to use curl -I --fail
instead, and check for exit code 22
.
If you're doing this in a Python script, it could look like:
try: subprocess.check_call(['curl', '-I', '--fail', url]) except subprocess.CalledProcessError as e: if e.returncode == 22: (do something)
The second is to actually ask just the HTTP status code, like this:
$ curl -s -I -o /dev/null -w '%' $bad-url 403