shell script: get server info from curl

2019-05-21 19:12发布

问题:

i need some help getting the server info from a curl call done in a shell script.

In my script, I iterate over several URLs in a list and do a cURL on each URL.

But I struggle getting the server information as it is not in a static position of the result of cURL.

>curl -I -s $temp

where, $temp is some arbitrary URL, e.g. example.org

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html
Date: Mon, 03 Feb 2014 14:35:39 GMT
Etag: "359670651"
Expires: Mon, 10 Feb 2014 14:35:39 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (iad/19AB)
X-Cache: HIT
x-ec-custom-error: 1
Content-Length: 1270

Above the result for example.org.

Question: how do I extract the part where it says server?
The result should be such that

>echo $server 

will yield (so basically the entire rest of the line after the "Server: ")

ECS (iad/19AB)

Thanks a bunch!

回答1:

Using awk:

server=$(curl -I -s http://example.org | awk -F': ' '$1=="Server"{print $2}')
echo "$server"
ECS (cpm/F858)

OR else you can use grep -oP:

server=$(curl -I -s http://example.org | grep -oP 'Server: \K.+')
echo "$server"
ECS (cpm/F858)


标签: regex shell curl