Bash Скрипт - Сравнение даты последнего изменения - PullRequest
0 голосов
/ 16 апреля 2020

Попытка сопоставить вывод date в том же формате, что и curl дата последнего изменения: Last-Modified: Thu, 16 Apr 2020 08:14:26 GMT, с фильтром Awk, как в приведенном ниже сценарии, это 16 Apr 2020 09:27:51. Дата последнего изменения локального файла будет сравниваться с датой последнего изменения удаленного файла. Этот сценарий кажется нестабильным, и я рассматриваю сравнение строк даты в текстовом файле, а не полагаться на обратную связь заголовка Http?

Мне известна опция -z в curl, но я хочу дать пользователю опции через операторы 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]

Ответы [ 2 ]

1 голос
/ 17 апреля 2020

Я переписал сценарий без использования команды оболочки date, которая не переносима в MacOS и GNU Linux. Вместо этого я использую ls, надеясь, что эта команда действительно переносима. Пожалуйста, проверьте:

#!/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
0 голосов
/ 16 апреля 2020

Попробуйте:

#!/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"

## let's compute timestamps
remote_last_modified_timestamp=$(date --date="$remote_last_modified_date" +"%s")
local_last_modified_timestamp=$(date +'%s' -r "${local_file}")

if [ $local_last_modified_timestamp -lt $remote_last_modified_timestamp ] ; then
  echo "A new version is available"
  set of commands
else
  echo "latest update already installed"
fi
...