Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created November 20, 2019 22:03
Show Gist options
  • Save rawiriblundell/4fd534c2ee8679dd979605cad19b3ffc to your computer and use it in GitHub Desktop.
Save rawiriblundell/4fd534c2ee8679dd979605cad19b3ffc to your computer and use it in GitHub Desktop.
A script to update shellcheck when using the precompiled binary
#!/bin/bash
# A quick script to automate upgrading the shellcheck binary
# Terse assign our default proxy settings if required
#: "${http_proxy:=http://proxy.company.org:3128}"
#: "${https_proxy:=$http_proxy}"
trap "rm -rf /tmp/shellcheck-latest*" EXIT
die() {
tput setaf 1
printf '%s\n' "$@" >&2
tput sgr0
exit 1
}
# Function to convert a semantic version number to an integer
# This zero-pads the second and third number, just in case
# either of those numbers goes into double digits
# This allows comparisons between e.g. 2.5.5 and 2.10.1 (i.e. 20505 < 21001)
sem_to_int() {
awk -F '.' '{printf "%i%02i%02i" , $1, $2, $3 }'
}
# If an existing version is in PATH, get its version and path
if command -v shellcheck >/dev/null 2>&1; then
curVersion=$(shellcheck -V | awk -F ': ' '/^version/{print $2}')
curVersionInt=$(sem_to_int <<< "${curVersion}")
# The following isn't used, the idea is to potentially deal with install paths
# that aren't /usr/bin e.g. /usr/local/bin or /bin or anything really...
# In this case, we're expecting /usr/bin for a quick script,
# so we don't need to build this extra logic and handling
#curLocation=$(command -v shellcheck)
fi
# Open a subshell to contain our filesystem moves
(
cd /tmp || die "[ERROR]: Could not cd into '/tmp'"
# Remove any existing copy (i.e. prior download) of the latest binary
rm -f shellcheck-latest.linux.x86_64.tar.xz 2>/dev/null
# Grab the latest version
wget https://shellcheck.storage.googleapis.com/shellcheck-latest.linux.x86_64.tar.xz ||
die "[ERROR]: Unable to download latest tar.xz file, please investigate"
# Extract it
tar -xf shellcheck-latest.linux.x86_64.tar.xz
cd shellcheck-latest || die "[ERROR]: Could not cd into './shellcheck-latest'"
# Get the latest version
newVersion=$(./shellcheck -V | awk -F ': ' '/^version/{print $2}')
newVersionInt=$(sem_to_int <<< "${newVersion}")
# If there is a current version, compare and act
if (( newVersionInt == curVersionInt )); then
printf -- '%s\n' "Latest version (${curVersion}) already installed, exiting..."
exit 0
elif (( newVersionInt > curVersionInt )); then
printf -- '%s\n' "Older version (${curVersion}) found, upgrading to ${newVersion}..."
printf -- '%s\n' "Copying /usr/bin/shellcheck to /usr/bin/shellcheck.${curVersion}"
cp /usr/bin/shellcheck /usr/bin/shellcheck."${curVersion}"
printf -- '%s\n' "Copying shellcheck (${curVersion}) to /usr/bin/shellcheck"
install -o root -g root -m 755 shellcheck /usr/bin
printf -- '%s\n' "Upgrade complete"
exit 0
# Otherwise, just copy the binary into /usr/bin
else
install -o root -g root -m 755 shellcheck /usr/bin
fi
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment