Reputation: 59
I need to check if the remote file exists based on the url response by doing:
curl -u myself:XXXXXX -Is https://mylink/path/to/file | head -1
What can give something like these:
'HTTP/1.1 200 OK
'
or
'HTTP/1.1 404 Not Found
'
Now, I want to extract the http status code like 200
from the resulting string above and assign the number to a variable. How can I do that?
Upvotes: 1
Views: 5370
Reputation: 33991
Nice and simple:
curl --output /dev/null --silent --head --fail http://google.com
Upvotes: 0
Reputation: 531758
Use the -o
option to send the headers to /dev/null
, and use the -w
option to output only the status.
$ curl -o /dev/null -u myself:XXXXXX -Isw '%{http_code}\n' https://mylink/path/to/file
200
$
If you intended to capture the status to a variable, you can omit the newline from the format.
$ status=$(curl ... -o /dev/null -Isw '%{http_code}' ...)
Upvotes: 7
Reputation: 3141
Use grep:
curl -u myself:XXXXXX -Is https://mylink/path/to/file | head -1 | grep -o '[0-9][0-9][0-9]'
Upvotes: 0