如何使用 shell 脚本从远程存储库下载最新文件?

How to download latest file from remote repository using shell script?

我有一个存储发布版本的远程存储库。我想要做的是编写一个 shell 脚本来下载最新版本(一个 .zip 文件)。存储库网页如下所示:

1.0.0
  |---> 1.0.0.zip(link)

1.0.1
  |---> 1.0.1.zip(link)

1.0.2
  |---> 1.0.2.zip(link)

1.0.x是一个目录,里面有一个zip文件。每天都会有不止一个或没有发布的build。基本上我想构建一个 URL like

http://path/to/file/1.0.x.zip

有了x会自动知道网页上的最新数字,这样wget就很容易使用了。

你或许可以这样做:

#!/bin/bash

# base path to server
base="http://path/to/file/"
# get files contained in base dir
lynx -dump -listonly "${base}" | grep http | grep '1\.0\.[0-9]+\.zip' | awk '{print }' > .tmpfiles
# get latest version number (x)
version=$(awk -F'.' '{ print  }' .tmpfiles | sort | tail -n1)
# get filename of latest build
latest=$(grep "1.0.${version}" .tmpfiles)
# download latest file
wget -qO - "${base}${latest}" > .tmp.zip && unzip .tmp.zip

您可以轻松修改它,以便将版本号作为参数传递给脚本。

但是如果构建存储在 FTP 服务器上,那么您可以创建一个简单的 FTP 脚本。