curl 检查文件是否更新而不是下载 - 执行 bash(或 python)脚本

Sto*_*hov 4 bash curl openelec

我有点问题。

我有一个文件,托管在远程服务器 ( http://mywebsite/file.zip ) 上。我还有一些嵌入式 linux 机器(运行 openelec OS)。这些盒子的命令相当有限,但它们仍然具有基本的 - curl、bash 等。它们都存储了文件(/storage/file.zip)

我想要做的是 - 我需要设置一个脚本,该脚本在设备完全启动后的第一分钟内执行,并且它可能会使用 curl 检查远程服务器文件(mywebsite/file.zip)是否为比本地的 (/storage/file.zip) 新,并且如果它更新而不是下载它 - 它需要执行一个 bash 脚本 (/storage/scripts/script.sh)

我通常使用这个命令“curl -o /storage/file.zip -z /storage/file.zip http://website/file.zip ”但我不知道如何让它执行脚本,而不是下载文件。甚至不确定这是否可能。

非常感谢所有帮助!

另外,为了确保 - 只有在 localfile 比 remotefile 旧时才需要执行。如果 localfile 比 remotefile 新 - 它不需要执行脚本,因为执行的脚本也在从服务器下载远程文件,因此在执行后 - localfile 将具有更新的时间戳,如果未指定,该脚本仅在较新的远程文件上执行 - 它可能会以无限循环结束。

小智 5

无需查看文件名,您可以相信您的 HTTP 服务器会告诉您文件最后一次更改是什么时候,并采取相应措施。

#!/bin/bash

remote_file="http://mywebsite/file.zip"
local_file="/storage/file.zip"

modified=$(curl --silent --head $remote_file | \
             awk '/^Last-Modified/{print $0}' | \
             sed 's/^Last-Modified: //')
remote_ctime=$(date --date="$modified" +%s)
local_ctime=$(stat -c %z "$local_file")
local_ctime=$(date --date="$local_ctime" +%s)

[ $local_ctime -lt $remote_ctime ] && /storage/scripts/script.sh

# end of file.
Run Code Online (Sandbox Code Playgroud)