linux – 如何使用Bash解析HTTP头?

我需要从使用curl的网页标题中获取2个值.我已经能够使用以下方法单独获取值:

response1=$(curl -I -s http://www.example.com | grep HTTP/1.1 | awk {'print $2'})
response2=$(curl -I -s http://www.example.com | grep Server: | awk {'print $2'})

但我无法弄清楚如何使用单个curl请求单独grep值,如:

response=$(curl -I -s http://www.example.com)
http_status=$response | grep HTTP/1.1 | awk {'print $2'}
server=$response | grep Server: | awk {'print $2'}

每次尝试都会导致错误消息或空值.我确信这只是一个语法问题.

最佳答案
完全bashsolution.演示如何在不需要awk的情况下轻松解析其他标头:

shopt -s extglob # Required to trim whitespace; see below

while IFS=':' read key value; do
    # trim whitespace in "value"
    value=${value##+([[:space:]])}; value=${value%%+([[:space:]])}

    case "$key" in
        Server) SERVER="$value"
                ;;
        Content-Type) CT="$value"
                ;;
        HTTP*) read PROTO STATUS MSG <<< "$key{$value:+:$value}"
                ;;
     esac
done < <(curl -sI http://www.google.com)
echo $STATUS
echo $SERVER
echo $CT

生产:

302
GFE/2.0
text/html; charset=UTF-8

根据RFC-2616,HTTP标头的建模如“Standard for the Format of ARPA Internet Text Messages” (RFC822)所述,其中明确说明了第3.1.2节:

The field-name must be composed of printable ASCII characters
(i.e.,characters that have values between 33. and 126.,
decimal,except colon). The field-body may be composed of any
ASCII characters,except CR or LF. (While CR and/or LF may be
present in the actual text,they are removed by the action of
unfolding the field.)

所以上面的脚本应该捕获任何RFC- [2] 822兼容的头,但有一个明显的例外,即folded headers.

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...