我正在尝试创建一个CRON作业,该作业使用BASH脚本从其网站下载WhatsApp的最新版本,并通过我的网站提供。
到目前为止,我能够使用以下(省略用户代理部件)从网站获得版本号:
wget -q -O - "$@" whatsapp.com/android | grep -oP '(?<=Version )([d.]+)'
我可以使用以下命令下载APK:
wget http://www.whatsapp.com/android/current/WhatsApp.apk
那部分很好。我不知道的是,只有比服务器上的现有APK更新时如何下载APK。脚本应该如何?
由于我不是命令行专业人士,所以我想有比我当前方法更好的方法,因此,如果您有任何建议,我会非常感谢。
似乎您需要自己管理版本。我将在文件名中存储APK文件,例如WhatsApp_<version-number>_.apk
。因此,下载较新文件的脚本如下:
# Get the local version
oldVer=$(ls -v1 | grep -v latest | tail -n 1 | awk -F "_" '{print $2}')
# Get the server version
newVer=$(wget -q -O - "$@" whatsapp.com/android | grep -oP '(?<=Version )([d.]+)')
# Check if the server version is newer
newestVer=$(echo -e "$oldVern$newVer" | sort -n | tail -n 1)
#Download the newer versino
[ "$newVer" = "$newestVer" ] && [ "$oldVer" != "$newVer" ] && wget -O WhatsApp_${newVer}_.apk http://www.whatsapp.com/android/current/WhatsApp.apk || echo "The newest version already downloaded"
#Delete all files that not is a new version
find ! -name "*$newVer*" ! -type d -exec rm -f {} ;
# set the link to the latest
ln -sf $(ls -v1 | grep -v latest| tail -n1) latest