Skip to content

Instantly share code, notes, and snippets.

@mystix
Forked from rhukster/sphp.sh
Last active April 30, 2019 07:54
Show Gist options
  • Save mystix/d70d8d93d22bd46f88bfb6da4819e694 to your computer and use it in GitHub Desktop.
Save mystix/d70d8d93d22bd46f88bfb6da4819e694 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Creator: Phil Cook
# Modified: Andy Miller
brew_prefix=$(brew --prefix | sed 's#/#\\\/#g')
brew_array=("5.5","5.6","7.0","7.1","7.2", "7.3")
php_array=("[email protected]" "[email protected]" "[email protected]" "[email protected]" "[email protected]" "[email protected]")
php_installed_array=()
php_version="php@$1"
# Has the user submitted a version required
if [[ -z "$1" ]]
then
echo "usage: sphp version"; echo;
echo " version one of:" ${brew_array[@]};
exit
fi
# What versions of php are installed via brew
for i in ${php_array[*]}
do
if [[ -n "$(brew ls --versions "$i")" ]]
then
php_installed_array+=("$i")
fi
done
# Check that the requested version is supported
if [[ " ${php_array[*]} " == *"$php_version"* ]]
then
# Check that the requested version is installed
if [[ " ${php_installed_array[*]} " == *"$php_version"* ]]
then
# Switch Shell
echo "Switching to $php_version"
echo "Switching your shell"
for i in ${php_installed_array[@]}
do
if [[ -n $(brew ls --versions $i) ]]
then
brew unlink $i
fi
done
brew link --force "$php_version"
echo ""
php -v
echo ""
echo "All done!"
else
echo "Sorry, but $php_version is not installed via brew. Install by running: brew install $php_version"
fi
else
cho "Unknown version of PHP. PHP Switcher can only handle arguments of:" ${brew_array[@]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment