Trying to match the date
output to the same format as curl
last modified date which is: Last-Modified: Thu, 16 Apr 2020 08:14:26 GMT
, with the Awk filter as in the script below it is 16 Apr 2020 09:27:51
. The local file's last modified date will be compared with the remote file's last modified date. This script feels flaky and I am considering comparing date strings in a text file as opposed to relying on Http header feedback?
I am aware of the -z
option in curl, but I want to give the user options via the if
statements, it can't be fully automated update process, the user (admin) input is needed.
http header
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
script:
#!/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
error:
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]