[英]Bash multiple variable assignment
Assign HTTP status code and date from cURL output to RESP
and DATE
variables using one-liner command. 使用一线命令将cURL输出中的HTTP状态代码和日期分配给RESP
和DATE
变量。
[08 Nov 2017 19:28:44 GMT] 301
$ read -d "\n" RESP DATE <<< $(curl -sv https://google.com 2>&1 | egrep -i "< HTTP/2|< Date" | sed "s/< HTTP\/2 //;s/< date: //g"); echo "[$DATE] $RESP"
[
] 30108 Nov 2017 19:28:44 GMT
$
EDIT: 编辑:
Here's the full working command: 这是完整的工作命令:
$ read -d "\r" RESP DATE <<< $(curl -sv https://google.com 2>&1 | tr -d "\r" | egrep -i "< HTTP/2|< Date" | sed "s/< HTTP\/2 //;s/< date: //g"); echo "[$DATE] $RESP"
[Wed, 08 Nov 2017 19:57:33 GMT] 301
$
The output of curl
contains some \\r
characters, that's why the output looks messed up. curl
的输出包含一些\\r
字符,这就是为什么输出看起来混乱的原因。 You can get rid of it by inserting a tr -d '\\r'
into the pipeline after the curl
and before the egrep
. 您可以通过在curl
和egrep
之前在管道中插入tr -d '\\r'
来摆脱它。
Is it really important to read into RESP
and DATE
variables? 读入RESP
和DATE
变量真的很重要吗? You could use Awk to extract the interesting parts simpler, saner, and output directly in the desired format: 您可以使用Awk提取更有趣,更精巧的有趣部分,并直接以所需格式输出:
curl ... | tr -d '\r' | awk '/^< Date: / { date = substr($0, 9); print "[" date "] " resp } /^< HTTP\// { resp = $3 }'
How about awk: 怎么样awk:
curl --silent --head https://google.com | awk '
/^HTTP/ {code = $2}
/^Date:/ {
sub(/\r$/,"")
date = substr($0,7)
printf "[%s] %s\n", date, code
exit
}
'
Using an HTTP HEAD
request to minimize traffic. 使用HTTP HEAD
请求来最小化流量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.