shell获取curl的返回结果_Shell脚本:从curl获取服务器信息

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!

解决方案

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)


版权声明:本文为weixin_39870150原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。