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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值