【发布时间】:2020-07-29 14:46:16
【问题描述】:
尝试将date 输出匹配到与curl 上次修改日期相同的格式,即:Last-Modified: Thu, 16 Apr 2020 08:14:26 GMT,使用下面脚本中的Awk 过滤器是16 Apr 2020 09:27:51。本地文件的最后修改日期将与远程文件的最后修改日期进行比较。这个脚本感觉很不稳定,我正在考虑比较文本文件中的日期字符串,而不是依赖 Http 标头反馈?
我知道 curl 中的 -z 选项,但我想通过 if 语句给用户选项,它不能完全自动化更新过程,需要用户(管理员)输入。
http 标头
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 16 Apr 2020 08:43:13 GMT
Content-Length: 0
Connection: keep-alive
Last-Modified: Thu, 16 Apr 2020 08:14:26 GMT
Expires: Thu, 16 Apr 2020 08:44:13 GMT
Cache-Control: max-age=60
X-Cache-Status: MISS
X-Backend-ip: x.0.172.195
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Pragma: public
Cache-Control: public
Vary: Accept-Encoding
Accept-Ranges: bytes
脚本:
#!/bin/bash
local_file="/Users/usr/Desktop/file"
remote_file="www.someurl.com/file"
# current output format: 16 Apr 2020 08:14:26
remote_last_modified_date="$(curl -sI ${remote_file} | grep -E "Last-Modified:" | awk '{print $3,$4,$5,$6}' )"
# current output format: Thu Apr 9 18:15:30 SAST 2020
local_last_modified_date="$(date -r "$local_file" )"
parsed_remote_last_modified_date="$(date +%s -d "$remote_last_modified_date")"
parsed_local_last_modified_date="$(date -r "$local_file")"
echo "local "$parsed_local_last_modified_date""
echo "remote "$parsed_remote_last_modified_date""
if [ ${parsed_local_last_modified_date} -lt
${parsed_remote_last_modified_date} ] ; then
echo "A new version is available"
else
echo "latest update already installed"
fi
错误:
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
【问题讨论】:
-
感谢分享您尝试过的代码,继续努力。除了努力分享,我们还鼓励用户添加输入示例和预期输出示例,因此请在您的问题中添加相同内容并让我们知道。
-
您是否尝试更改日期格式
date '+%a, %d %b %T GMT'它将返回您所说的Thu, 16 Apr 2020 08:14:26 GMTLast-Modified -
它给出了同样的错误,我在 local_last_modified_date 变量中看不到语法错误。尝试了 date -rf 选项 - 也没有用。
-
试试
local_last_modified_date="$( date -r "${local_file}" '+%D %T' ) -
演示:
$date -r "file.txt" '+%D %T'--> 04/16/20 11:36:19