Skip to content

Instantly share code, notes, and snippets.

@marcobellaccini
Last active September 20, 2018 15:10
Show Gist options
  • Save marcobellaccini/1655cfe7e6fd8eeecac8352ca972aad3 to your computer and use it in GitHub Desktop.
Save marcobellaccini/1655cfe7e6fd8eeecac8352ca972aad3 to your computer and use it in GitHub Desktop.
A bash script to download and install the latest Redis stable version, also checking its hash.
#!/bin/sh
#
# A shell script to download and install the latest Redis stable
# version, also checking its hash.
# As suggested at:
# https://redis.io/download#how-to-verify-files-for-integrity
# the hash check is performed using this file:
# https://raw.githubusercontent.com/antirez/redis-hashes/master/README
#
# Author: Marco Bellaccini - marco.bellaccini[at!]gmail.com
#
# License:
# Creative Commons CC0 1.0
# https://creativecommons.org/publicdomain/zero/1.0/legalcode
#
# check if running as root
if [ "$(id -u)" -ne 0 ]
then
echo "Sorry, this script must be run as root."
exit 100;
fi
# get last line of the Redis hash file from GitHub
LASTREL=$(wget -qO- https://raw.githubusercontent.com/antirez/redis-hashes/master/README | tail -n 1)
# get the name of the latest Redis tarball
TARBALL=$(echo "$LASTREL" | cut -d " " -f 2)
# get resulting folder name (tarball name without extension)
FNAME="${TARBALL%.tar.gz}"
# check whether folder name is complete
echo "$FNAME" | grep "^redis-"
if [ "$?" -ne "0" ]
then
FNAME="redis-$FNAME"
fi
# get expected sha256 hash
EHASH=$(echo "$LASTREL" | cut -d " " -f 4)
# get tarball url
URL=$(echo "$LASTREL" | cut -d " " -f 5)
# check that url points to Redis website or Antirez GitHub
echo "$URL" | grep "^http://download.redis.io\|^https://github.com/antirez/"
if [ "$?" -ne "0" ]
then
echo "Error: invalid tarball url."
exit 1;
fi
# get tarball
wget "$URL"
# get sha256 hash
HASH=$(sha256sum "$TARBALL" | cut -d " " -f 1)
# check hash
if [ "$HASH" != "$EHASH" ]
then
echo "Error: invalid tarball hash."
exit 1;
fi
# extract tarball
tar xzf "$TARBALL"
# build and install
cd "$FNAME"
make && make install
if [ "$?" -ne "0" ]
then
echo "Error: cannot build and install redis."
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment