我正在使用curl来检索http_code size_header redirect_url和网站标题:
#!/bin/bash
FILE="$1"
while read LINE; do
curl -H 'Cache-Control: no-cache' -i -s -k -o >(perl -l -0777 -ne 'print $1 if /<title.*?>s*(.*?)s*</title/si') --silent --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$LINE"
echo " $LINE"
done < ${FILE}
但我也想从响应中检索前 20 个字符以获得更多信息。
这个想法是获得这个输出
%{http_code} %{size_header} %{redirect_url} $website_title $website_first_20_bytes
我只需要将 $website_first_20_bytes 添加到输出中。我怎样才能做到这一点?
PS:没有标题响应中的前 20 个字符。只有来源。
所以你可能就是这样的意思(我在输出中添加了一堆换行符等,你可以随心所欲地修剪(:
#!/bin/bash
FILE="$1"
while read -r LINE; do
# read the response to a variable
response=$(curl -H 'Cache-Control: no-cache' -s -k --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$LINE")
# get the title
title=$(sed -n 's/.*<title>(.*)</title>.*/1/ip;T;q'<<<"$response")
# read the write-out from the last line
read -r http_code size_header redirect_url < <(tail -n 1 <<<"$response")
printf "Status: %sn" "$http_code"
printf "Size: %sn" "$size_header"
printf "Redirect-url: %sn" "$redirect_url"
printf "Url: %sn" "$LINE"
printf "Title: %s" "$title"
# -c 20 only shows the 20 first chars from response
printf "Body: %s" "$(head -c 20 <<<"$response")"
done < ${FILE}