Skip to content

Instantly share code, notes, and snippets.

@calbrecht
Created August 10, 2013 09:49
Show Gist options
  • Save calbrecht/6199805 to your computer and use it in GitHub Desktop.
Save calbrecht/6199805 to your computer and use it in GitHub Desktop.
PHP Version Manager utilizing update-alternatives
#!/bin/bash
if [ "$EUID" != "0" ]; then
sudo $0 $@;
exit 1;
fi
BIN_DIR=/usr/bin
PHP_DIR=/usr/local/php;
PHP_VERSION=""
PHP_VERSION_DIR=""
_check_version() {
local VERSION=$1
if [[ "" = "$VERSION" ]] ; then
select VERSION in $PHP_DIR/[0-9]* ; do
if [[ ! "" = "$VERSION" ]] ; then
VERSION=$(basename $VERSION)
break
fi
done
fi
if [[ ! -d "$PHP_DIR/$VERSION" ]] ; then
echo "E: $PHP_DIR/$VERSION is not a directory"
exit 1
fi
PHP_VERSION=$VERSION
PHP_VERSION_DIR=$PHP_DIR/$PHP_VERSION;
}
_install(){
local FILE
local NAME
local COMMANDS
for FILE in $PHP_VERSION_DIR/bin/* ; do
NAME=$(basename $FILE)
if [[ "php" = "$NAME" ]] ; then
COMMANDS="--install $BIN_DIR/php php $FILE 100 $COMMANDS"
else
COMMANDS="$COMMANDS --slave $BIN_DIR/$NAME $NAME $FILE"
fi
done
update-alternatives $COMMANDS
}
_switch(){
update-alternatives --set php $PHP_VERSION_DIR/bin/php
}
_remove(){
update-alternatives --remove php $PHP_VERSION_DIR/bin/php
}
case "$1" in
install|switch|remove)
_check_version $2 && _$1
;;
*)
echo "Usage: $0 (install|switch|remove)";
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment