[shell]curl结果获取http header的问题
发布时间:2020-12-15 09:21:57 所属栏目:安全 来源:网络整理
导读:在通过curl请求http获取response header时, 发现字符串拼接一个问题。 比如以下程序: hadoop @1 :~$ ct= $( curl -s -I http: //www.baidu.com | grep Content-Type | awk '{print $2}') hadoop @1 :~$ echo $ct text /htmlhadoop @1 :~$ echo $ct "_postfi
在通过curl请求http获取response header时, 发现字符串拼接一个问题。 hadoop@1:~$ ct=$(curl -s -I http://www.baidu.com | grep Content-Type | awk '{print $2}')
hadoop@1:~$ echo $ct
text/html
hadoop@1:~$ echo $ct"_postfix"
_postfixl
最后一行输出竟然是: shopt -s extglob # 识别正则,用于后面去掉空格
function grepCT()
{
while IFS=':' read key value; do
# 去掉"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 $1)
echo $CT
}
ct=$(grepCT http://www.baidu.com)
echo $ct"_postfix"
执行shell文件: hadoop@1:~$ bash test.sh
text/html_postfix (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |