Issue
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]
Solution
I have rewritten the script without using the date
command of the shell, which is not portable across MacOS and GNU Linux. I use ls
instead, hoping this command is actually portable. Please check:
#!/bin/bash
## variables commented out since we don't have access to theirs values:
#remote_file="www.someurl.com/file"
#remote_last_modified_date="$(curl -sI ${remote_file} | grep -E "Last-Modified:" | awk '{print $3,$4,$5,$6}' )"
## let's suppose we get next result into our variables:
local_file="test"
remote_last_modified_date="16 Apr 2020 08:14:26"
## we don't use the date command, not portable across MacOS and GNU Linux
## we use ls command with the option --time-style instead
## rewrite dates to allow comparison in format +"%Y-%m-%d_%H:%M:%S"
local_sortable_date=$(ls -l --time-style=+"%Y-%m-%d_%H:%M:%S" "$local_file"| cut -d ' ' -f 6)
remote_year=${remote_last_modified_date:7:4}
remote_month_name=${remote_last_modified_date:3:3}
remote_month=$(echo $remote_month_name |
sed 's/Jan/01/;s/Feb/02/;s/Mar/03/;s/Apr/04/;s/May/05/;s/Jun/06/;
s/Aug/08/;s/Sep/09/;s/Oct/10/;s/Nov/11/;s/Dec/12/')
remote_day=${remote_last_modified_date:0:2}
remote_time=${remote_last_modified_date:12:8}
remote_sortable_date=${remote_year}-${remote_month}-${remote_day}_$remote_time
if [[ $remote_sortable_date > $local_sortable_date ]] ; then
echo "A new version is available"
set of commands
else
echo "latest update already installed"
fi
Answered By - Pierre François Answer Checked By - Gilberto Lyons (WPSolving Admin)