Skip to content

Instantly share code, notes, and snippets.

@divoxx
Created July 16, 2009 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divoxx/148114 to your computer and use it in GitHub Desktop.
Save divoxx/148114 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Utility script that manage binary versioned symlinks.
#
# Supposing in /usr/local/bin, there is two executables: ruby1.8 and ruby1.9
# and /usr/local/bin/ruby is a symlink to ruby1.8.
#
# Running the scripts would do the following:
#
# $ choose ruby list
# Available ruby versions:
# 1.8 [Current]
# 1.9
#
# $ sudo choose ruby set 1.9
# $ choose ruby list
# Available ruby versions:
# 1.8
# 1.9 [Current]
#
# To install simply copy to a folder on your path.
#
# Author: Rodrigo Kochenburger <divoxx@gmail.com>
BIN_NAME="$1"
COMMAND="$2"
VERSION="$3"
BIN_PATH="/usr/local/bin/$BIN_NAME"
if [ -z $BIN_NAME ] || [ -z $COMMAND ] || ([ $COMMAND == "set" ] && [ -z $VERSION ]); then
echo "Usage: ./choose.sh <bin_name> [ list | set <version> ]"
exit 1
fi
function extract_versions {
local version
for file in "$@"; do
version=`echo $file | grep -E "$BIN_NAME[0-9](.[0-9])*" | grep -Eo "[0-9](.[0-9])*"`
echo $version
done
}
if [ -a $BIN_PATH ]; then
CURR_VER=$(extract_versions `readlink $BIN_PATH`)
fi
function list_versions {
echo "Available $BIN_NAME versions:"
for version in $(extract_versions $BIN_PATH*); do
echo -n " $version"
if [[ $CURR_VER == $version ]]; then
echo -n " [Current]"
fi
echo ""
done
}
function set_version {
rm -rf $BIN_PATH
ln -s "${BIN_PATH}${VERSION}" $BIN_PATH
}
case $COMMAND in
list) list_versions;;
set) set_version;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment