Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redis Quickstart
#!/bin/bash
echo "$(tput setaf 6)$(tput bold)○ Checking Redis...$(tput sgr0)"
# Check if Redis CLI is present
if ! [ -x "$(command -v redis-cli)" ]; then
# If not, provide users an option to download
echo " $(tput setaf 3)$(tput bold)• Redis is not installed or on the path$(tput sgr0)"
read -p " Do you want to install it? $(tput dim)(Y/N)$(tput sgr 0) " prompt
if [[ $prompt =~ [yY] ]]; then
# Check if tools required to download and install redis are present
if ! [ -x "$(command -v make)" -a -x "$(command -v wget)" -a -x "$(command -v tar)" ]; then
# If not, we cannot do anything unless user installs the tools
echo " $(tput setaf 1)$(tput bold)Please make sure you have these packages installed: make, wget, tar$(tput sgr0)"
exit 1
fi
# Check if .redis directory exists, helps in resuming the installation process
# by not downloading redis again
if [ -e ~/.redis ]; then
# Redis is already downloaded and present in preconfigured location
echo " $(tput setaf 6)$(tput bold)• Picked up Redis location...$(tput sgr0)"
# Saving current location to return to after installation
contextdir=$(pwd)
echo " $(tput setaf 5)$(tput bold)• Checking Redis Build Status...$(tput sgr0)"
cd ~/.redis
# Checking if redis is built
if [ -e src/redis-cli -a -e src/redis-server ]; then
echo " $(tput setaf 6)$(tput bold)• Redis is built!$(tput sgr0)"
else
# Build redis
echo " $(tput setaf 5)$(tput bold)• Building Redis...$(tput sgr0)"
make -j 4
# Move back to the folder we came from
cd $contextdir
fi
else
# Redis folder not present, we will now download it
echo " $(tput setaf 5)$(tput bold)• Downloading Redis...$(tput sgr0)"
wget -c 'http://download.redis.io/redis-stable.tar.gz'
echo " $(tput setaf 5)$(tput bold)• Unpacking Redis...$(tput sgr0)"
tar xzf redis-stable.tar.gz
echo " $(tput setaf 5)$(tput bold)• Building Redis...$(tput sgr0)"
# Build redis
cd redis-stable
make -j 4
cd ..
# Move redis installation to preconfigured location
echo " $(tput setaf 5)$(tput bold)• Moving Redis to ~/.redis...$(tput sgr0)"
mv redis-stable ~/.redis
echo " $(tput setaf 5)$(tput bold)• Removing Redis tar...$(tput sgr0)"
rm redis-stable.tar.gz
fi
# Redis is both present and built, add to PATH
echo " $(tput setaf 5)$(tput bold)• Adding Redis to PATH...$(tput sgr0)"
path='export PATH=~/.redis/src:$PATH'
echo $path >> ~/.profile
# To allow PATH change to register in this terminal session, we source the updated profile
source ~/.profile
echo " $(tput setaf 2)$(tput bold)• PATH Set!$(tput sgr0)"
else
# User declined the download, not much we can do
echo " $(tput setaf 1)$(tput bold)Skipping the download. Please download it yourself. Bye!$(tput sgr0)"
exit 1
fi
fi
# Redis is found in PATH, checking instance
echo " $(tput setaf 2)$(tput bold)• Redis found!$(tput sgr0)"
echo " $(tput setaf 6)$(tput bold)○ Checking Redis Instance...$(tput sgr0)"
if [ $(redis-cli ping) ]; then
echo " $(tput setaf 2)$(tput bold)• Redis already running!$(tput sgr0)"
else
echo " $(tput setaf 5)$(tput bold)• Starting Redis...$(tput sgr0)"
redis-server &
echo " $(tput setaf 2)$(tput bold)• Redis started - $(redis-cli ping)!$(tput sgr0)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.