Skip to content

Instantly share code, notes, and snippets.

@skcin7
Created January 22, 2021 23:36
Show Gist options
  • Save skcin7/0bef9dac69b7893deadb6456fd6626d1 to your computer and use it in GitHub Desktop.
Save skcin7/0bef9dac69b7893deadb6456fd6626d1 to your computer and use it in GitHub Desktop.
PHP Version Switcher (Bash Shell Script)
#!/bin/bash
#================================================================
# HEADER
#================================================================
#% SYNOPSIS
#+ ${SCRIPT_NAME} args ...
#%
#% DESCRIPTION
#% A simple script to switch the current PHP version being used.
#% used. The switch is reflected system-wide, including in the
#% terminal, and any Apache/NGINX web server configurations
#% (which may or may not be running).
#%
#% OPTIONS
#% -h, --help Print this help
#% -v, --version Print script information
#%
#% EXAMPLES
#% ${SCRIPT_NAME}
#% ${SCRIPT_NAME} 7.4
#%
#================================================================
#- IMPLEMENTATION
#- version ${SCRIPT_NAME} (nicholas-morgan.com) v1.0.0
#- author Nick Morgan
#- copyright Copyright (c) http://nicholas-morgan.com
#- date January 22, 2021
#- license GNU General Public License
#- script_id 0
#-
#================================================================
# HISTORY
# 2021/01/22 : v1.0.0 : Script creation and published
#
#================================================================
# END_OF_HEADER
#================================================================
# Some colors to help make the script stand out a bit!
NOCOLOR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
echo -e "${YELLOW}";
echo "--------------------";
echo "PHP Version Switcher";
echo "--------------------";
echo -e "${NOCOLOR}";
# In case the PHP version has already been changed in the current bash session, reload the bash profile first to ensure the current PHP version is correct within this script.
source ~/.bash_profile
echo -e "The current PHP version running is ${GREEN}$PHP_VERSION${NOCOLOR}.";
echo "";
# Determine what PHP version should be switched to. This is done by examining what the first input argument of the script is, or if no argument was passed, asking the user what it should be.
if [[ "$1" != "" ]]; then
NEW_PHP_VERSION="$1"
else
echo -e "What PHP version should be switched to? ${YELLOW}[7.4, 7.3, 7.2]${NOCOLOR}";
read NEW_PHP_VERSION;
echo "";
fi
echo -e "You have specified to switch the PHP version to ${YELLOW}$NEW_PHP_VERSION${NOCOLOR}.";
echo "";
# Some error checking to ensure it's not being changed to the current version
if [ $NEW_PHP_VERSION = $PHP_VERSION ]; then
echo -e "${RED}Can not switch PHP version to $NEW_PHP_VERSION, because that is already the current PHP version!${NOCOLOR}";
exit 1;
fi
# Execute the PHP version change...
if [ $NEW_PHP_VERSION = "7.4" ] || [ $NEW_PHP_VERSION = "7.3" ] || [ $NEW_PHP_VERSION = "7.2" ]; then
echo -e "${YELLOW}Switching PHP version to $NEW_PHP_VERSION...${NOCOLOR}";
echo "";
# Ensure all possible php services with brew are stopped before starting the service for the new version
sudo echo "Stopping all possible currently running php services for all possible PHP versions...";
sudo brew services stop php@7.2;
sudo brew services stop php@7.3;
sudo brew services stop php@7.4;
echo "Done!";
echo "";
# Start the service for the correct PHP version being changed to
echo "Starting the service for the new PHP version $NEW_PHP_VERSION...";
if [ $NEW_PHP_VERSION = "7.4" ]; then
sudo brew services start php@7.4;
fi
if [ $NEW_PHP_VERSION = "7.3" ]; then
sudo brew services start php@7.3;
fi
if [ $NEW_PHP_VERSION = "7.2" ]; then
sudo brew services start php@7.2;
fi
echo "Done!";
echo "";
#export PHP_VERSION=7.4
# Store the new PHP version in the file which sets the PHP version variable in the .bash_profile. A separate file is used for this.
> ~/.bash_php_version
echo "export PHP_VERSION=$NEW_PHP_VERSION" > ~/.bash_php_version
# Reload the bash_profile so that the new PHP_VERSION environment variable will be reflected (without having to open a new bash session)
#source ~/.bash_profile
echo -e "${GREEN}The PHP version has been switched to $NEW_PHP_VERSION!${NOCOLOR}";
echo -e "${YELLOW}Warning:${NOCOLOR} The new PHP version may not be reflected in the current bash session. So, open a new one, or manually run 'source ~/.bash_profile' to reflect the updated version in the current bash session.";
echo "";
# Reflect the updated PHP version in the current bash session.
#set PHP_VERSION="$NEW_PHP_VERSION";
#source ~/.bash_profile # Not working for some reason, but works if you run it manually after the script is done?
else
echo -e "${RED}The PHP version $NEW_PHP_VERSION you entered was not recognized, so no changes were made!${NOCOLOR}";
exit 2;
fi
exit 0; # successfully executed!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment