Automatically updating your .bashrc when you log into a server


I am a long time bash user and have found numerous aliases and shell functions that allow me to be more productive at the prompt. Depending on how you manage (configuration management, NFS mounted home directories, etc.) ${HOME} making sure your bashrc gets updated when you find a cool new feature can be a pain. I was tinkering around last weekend and thought about adding a block of code to my bashrc to run curl to grab the latest version of my bashrc from github. The following short code block works for my needs:

# Location to pull bashrc from
bashrc_source="https://raw.githubusercontent.com/Matty9191/bashrc/master/bashrc"

# Take precaution when playing with temp files
temp_file=$(mktemp /tmp/tmp.XXXXXXXX)
touch ${temp_file}

curl -s -o ${temp_file} ${bashrc_source}
RC=$?

if [ ${RC} -eq 0 ]; then
version=$(head -1 ${temp_file} | awk -F'=' '/VERSION/ {print $2}')

if [ "${version}" -gt "${VERSION}" ]; then
echo "Upgrading bashrc from version ${VERSION} to ${version}"
cp ${HOME}/.bashrc ${HOME}/.bashrc.bak.$(/bin/date "+%m%d%Y.%S")
mv ${temp_file} ${HOME}/.bashrc
fi
else
echo "Unable to retrive bashrc from ${bashrc_source}"
rm ${temp_file}
fi

If a new version is available (a VERSION variable tracks the release #) I get the following output when I log in:

***Upgrading bashrc from version 44 to 46***

If github is unavailable due to a service issue or a firewall won’t let me out the script will let me know:

***Unable to retrieve bashrc from
https://gik/Matty9191/bashrc/master/bashrc***

How are you keeping your shell profiles up to date? Let me know in the comment section.

This article was posted by Matty on 2017-01-25 09:05:00 -0400 -0400