Skip to content

Instantly share code, notes, and snippets.

@a-kbv
Last active May 23, 2024 15:25
Show Gist options
  • Select an option

  • Save a-kbv/5b98928b5358ae45602bf16a9be09963 to your computer and use it in GitHub Desktop.

Select an option

Save a-kbv/5b98928b5358ae45602bf16a9be09963 to your computer and use it in GitHub Desktop.
ubuntu/debian PHP version switcher
#!/bin/bash
# Ensure the script is not sourced
if [[ "$(basename -- "$0")" =~ ^(bash|sh)$ ]]; then
echo "Please run the script directly, not with 'source' or '.'"
return 1
fi
# Obtain the directory of the current script
SCRIPT_DIR="$(cd "$(dirname "$(realpath "${BASH_SOURCE[0]}")")" && pwd -P)"
cd "$SCRIPT_DIR"
# Set color codes for better readability
BOLD="$(tput bold)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
WHITE="$(tput setaf 7)"
RESET="$(tput sgr0)" # reset all attributes
# Spinner animation function
spin() {
local spinner=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
local end=$((SECONDS+1))
while [ $SECONDS -lt $end ]; do
for i in "${spinner[@]}"; do
echo -ne "\r$i"
sleep 0.1
if [ $SECONDS -ge $end ]; then
echo -ne "\r "
return
fi
done
done
}
# Function: Add alias to .bashrc if not already added
add_bashrc_alias() {
local ALIAS_NAME="chphp"
local MARKER="# Added by PHP switch script"
if ! grep -q "$MARKER" "$HOME/.bashrc"; then
local DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
local SCRIPT_PATH="${DIR}/$(basename ${BASH_SOURCE[0]})"
{
echo "$MARKER"
echo "alias ${ALIAS_NAME}='${SCRIPT_PATH}'"
echo "$MARKER End"
} >> "$HOME/.bashrc"
echo "${GREEN}${BOLD}Alias '${ALIAS_NAME}' has been added to .bashrc.${RESET} You may need to restart your terminal or run 'source ~/.bashrc'."
else
echo "Alias '${ALIAS_NAME}' is already added to .bashrc."
fi
}
# Adding alias to .bashrc
add_bashrc_alias
# Source .bashrc to refresh the terminal session
source "$HOME/.bashrc"
# Fetch available PHP versions
php_versions=$(for x in /etc/php/*/; do basename "$x"; done)
php_versions_array=($php_versions)
array_length=${#php_versions_array[@]}
# Get current PHP version
current_php_version_command=$(php -v)
current_php_version=${current_php_version_command:4:3}
# Display header
clear
echo "${BLUE}${BOLD}"
echo "╔═════════════════════════════════════════════════╗"
echo "║ Welcome to the PHP Switcher ║"
echo "╚═════════════════════════════════════════════════╝"
echo "${RESET}"
# Display list of available PHP versions
echo "${YELLOW}Select PHP Version from the list${RESET}"
echo "${YELLOW}════════════════════════════════════════${RESET}"
for ((i = 0; i < array_length; i++)); do
index=$((i + 1))
echo "${BOLD}${index}. php${php_versions_array[$i]}${RESET}"
done
echo "${YELLOW}════════════════════════════════════════${RESET}"
# Prompt user to select PHP version
pattern="^[0-9]+$"
selected_index=""
while [[ ! $selected_index =~ $pattern || -z ${php_versions_array[selected_index - 1]} ]]; do
echo "${YELLOW}Please enter a valid input${RESET}"
read selected_index
done
# Spinner while switching PHP version
spin
# Switch to the selected PHP version
new_php_version=${php_versions_array[$selected_index - 1]}
# Display section header
echo "${BLUE}${BOLD}Switching to PHP${new_php_version}...${RESET}"
# Execute PHP version switch commands
sudo a2dismod php$current_php_version &> /dev/null
sudo a2enmod php$new_php_version &> /dev/null
sudo systemctl restart apache2 &> /dev/null
sudo update-alternatives --set php /usr/bin/php$new_php_version &> /dev/null
sudo update-alternatives --set phar /usr/bin/phar$new_php_version &> /dev/null
sudo update-alternatives --set phar.phar /usr/bin/phar.phar$new_php_version &> /dev/null
sudo update-alternatives --set phpize /usr/bin/phpize$new_php_version &> /dev/null
sudo update-alternatives --set php-config /usr/bin/php-config$new_php_version &> /dev/null
# Confirm the new PHP version
echo "${GREEN}${BOLD}"
echo "╔════════════════════════════╗"
echo "║ PHP has been switched to ║"
echo "║ ${WHITE}php${new_php_version}${GREEN} ║"
echo "╚════════════════════════════╝"
echo "${RESET}"
php -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment