我怎样才能得到一个HTTP状态code与AWK单行? [英] How can I get an HTTP status code with an AWK one-liner?

查看:137
本文介绍了我怎样才能得到一个HTTP状态code与AWK单行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印不仅仅是 HTTP状态code 对于网页使用cURL检索。是否有可能用AWK单行做到这一点?

I want to print just the HTTP status code for a web page retrieved using cURL. Is it possible to do this with an AWK one-liner?

推荐答案

下面的一行代码将读取从管道的HTTP头,并打印出状态code。

Solution

The following one-liner will read the HTTP header from a pipe, and print out the status code.

awk 'BEGIN {"curl -sI http://example.com" | getline; print "Status Code: " $2}'

有趣的方面

有对这种做法可能不是第一眼明显一些好东西。例如:

Interesting Aspects

There are a few nice things about this approach that may not be obvious at first glance. For example:


  • 这个解决方案是纯AWK;没有shell脚本或包装要求。

  • 由于我们只关心标题的第一线,我们不需要跟踪或比较NR跳过不良线。

  • $ 0被填充在一个BEGIN块,所以AWK不需要文件的说法。

这篇关于我怎样才能得到一个HTTP状态code与AWK单行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆