Skip to content

Instantly share code, notes, and snippets.

@leophys
Created November 15, 2020 16:29
Show Gist options
  • Save leophys/94a3aeb6ff21150ca69576de2642f03d to your computer and use it in GitHub Desktop.
Save leophys/94a3aeb6ff21150ca69576de2642f03d to your computer and use it in GitHub Desktop.
Script to update bat from the github release page for debian derivatives.
#!/usr/bin/env bash
if ! which curl > /dev/null; then
echo "You need to install curl"
exit 1
fi
if ! which jq > /dev/null; then
echo "You need to install jq"
exit 1
fi
download_and_install() {
curl -LJO ${DL_LINK}
dpkg -i *.deb
}
compare_versions() {
local major1=$(echo ${1}|awk -F"." '{print $1}')
local minor1=$(echo ${1}|awk -F"." '{print $2}')
local micro1=$(echo ${1}|awk -F"." '{print $3}')
local major2=$(echo ${2}|awk -F"." '{print $1}')
local minor2=$(echo ${2}|awk -F"." '{print $2}')
local micro2=$(echo ${2}|awk -F"." '{print $3}')
if [ ${major1} -gt ${major2} ]; then
return 0
elif [ ${major1} -eq ${major2} ] && [ ${minor1} -gt ${minor2} ]; then
return 0
elif [ ${major1} -eq ${major2} ] && [ ${minor1} -eq ${minor2} ] && [ ${micro1} -gt ${micro2} ] ; then
return 0
fi
return 1
}
CURRENT_VERSION=0.0.0
if which bat > /dev/null; then
CURRENT_VERSION=$(apt-cache show bat|grep Version|awk '{print $2}')
fi
echo "-> Current version: ${CURRENT_VERSION}"
ASSETS=$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/sharkdp/bat/releases 2>/dev/null)
DL_VERSION=$(echo $ASSETS|jq '.[0].tag_name'|sed -e 's/v//'|sed -e 's/"//g')
DL_LINK=$(echo ${ASSETS}|jq '.[0].assets[] | select(.name | contains("_amd64.deb")) | .browser_download_url'|grep -v musl|sed -e 's/"//g')
echo "-> Latest version found: ${DL_VERSION}"
if compare_versions ${DL_VERSION} ${CURRENT_VERSION}; then
TMPDIR=$(mktemp -d)
CURDIR=$PWD
cd ${TMPDIR}
download_and_install || true
cd ${PWD}
rm -rf ${TMPDIR}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment