Mac OS X 下利用脚本检测软件是否有更新

/

在 Mac OS X 下有时候很想知道哪些软件有更新,如果不想打开每个软件检查,或者浏览软件官网查看是否有新版本发布,可以使用专门的软件版本更新检查工具,比如 MacUpdate Desktop,除此之外,写个脚本也是不错的选择。

# !/usr/bin/env bash
vercomp() {
    [[ $1 == $2 ]] && return 1
    local IFS=.-_
    local i ver1=($1) ver2=($2)
    for (( i=${#ver1[@]}; i<${#ver2[@]}; i++ )); do
        ver1[i]=0
    done
    for (( i=0; i<${#ver1[@]}; i++ )); do
        [[ -z ${ver2[i]} ]] && ver2[i]=0
        (( 1${ver1[i]} > 1${ver2[i]} )) && return
        (( 1${ver1[i]} < 1${ver2[i]} )) && return 1
    done
    return 1
}

[[ $(uname -s) != 'Darwin' ]] && { echo 'This script only work on Mac OS X'; exit 1; }
record=$HOME/.appver
if [[ ! -f ${record} ]]; then
    echo "Please specify app name and url separated by comma in ${record}"
    exit 1
fi
len=$(awk -F',' '{max=(length($1)>max?length($1):max)} END {printf "%d%s",max,RS}' "${record}")
fail=()
while read line; do
    app=$(awk -F',' '{print $1}' <<< "${line}")
    url='http://www.macupdate.com/app/mac/'$(awk -F',' '{print $2}' <<< "${line}")
    rv=$(curl -s ${url} | grep 'twitter:data2' | awk -F'"' '{print $4}')
    if [[ -d /Applications/${app}.app ]]; then
        appPath=/Applications/${app}.app
    elif [[ -d ${HOME}/Applications/${app}.app ]]; then
        appPath=${HOME}/Applications/${app}.app
    else
        printf "%${len}s: doesn't install, skipping\n" "${app}"
        fail+=("${app}")
        continue
    fi
    lv=$(tr '\r' '\n' < "${appPath}/Contents/Info.plist" 2>/dev/null | xargs | sed 's@.*CFBundleShortVersionString<[^>]\+>\s*<[^>]\+>\([^<]\+\)<[^>]\+>.*@\1@')
    if [[ ${lv} =~ ^[0-9._-]+$ ]]; then
        if vercomp ${rv} ${lv}; then
            printf "$(tput setaf 3)%${len}s$(tput sgr 0): $(tput setaf 2)%s$(tput sgr 0) => $(tput setaf 1)%s$(tput sgr 0)\n" "${app}" "${lv}" "${rv}"
        else
            printf "%${len}s: up to date\n" "${app}"
        fi
    else
        printf "%${len}s: can't get installed version info, skipping\n" "${app}"
        fail+=("${app}")
        continue
    fi
done < "${record}"
#  deleting invalid line from record file
for i in "${fail[@]}"; do
    echo "=== Remove line about $i from ${record} ==="
    sed -i "/$i/d" "${record}"
done

上面这段脚本逐行检查 ~/.appver 中的软件在 MacUpdate 的版本,并与已安装的版本进行比较,如果有更新,则进行提示。

Alfred 2,34344/alfred
Carbon Copy Cloner,7032/carbon-copy-cloner
CleanApp,21453/cleanapp
Clearview,47325/clearview
Growl,15979/growl
Dropbox,28731/dropbox
Dash,40201/dash
Jettison,40766/jettison
Karabiner,25141/karabiner
Lyn,33371/lyn
PlistEdit Pro,14363/plistedit-pro
RegExRX,33164/regexrx
Sequel Pro,28473/sequel-pro
Sketch,35230/sketch
The Unarchiver,22774/the-unarchiver
VirtualBox,24801/virtualbox

~/.appver 中的每行由两部分组成,用逗号分割。第一部分是软件的名称,需要与 Applications 目录下的软件名称匹配。第二部分是软件在 www.macupdate.com 的 url 的最后一部分。

执行效果如下:

vertracker.sh

Comments