Last active
July 7, 2020 01:11
-
-
Save ToX82/7298f2aa460a5f23491a95b409336d1e to your computer and use it in GitHub Desktop.
A simple script that helps you choose which PHP version should be active - apache version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
color_off='\033[0m' # Text Reset | |
color_green='\033[1;32m' # Green text | |
versions=(/usr/bin/php*) | |
mapfile -t versions < <(printf '%s\n' "${versions[@]}" | egrep '[0-9]+\.[0-9]+' | cut -c 13-) | |
counter=0 | |
if [ $# -eq 0 ]; then | |
current=$(php -v | grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+") | |
current=${current:4:3} | |
echo "" | |
echo 'Installed PHP versions:' | |
for i in "${versions[@]}"; do | |
if [ "$current" == "$i" ]; then | |
echo -e "$(($counter + 1))) PHP $i ${color_green}<- current${color_off}" | |
else | |
echo "$(($counter + 1))) PHP $i" | |
fi | |
counter=$(($counter + 1)) | |
done | |
echo "" | |
echo 'Which version would you like to activate?' | |
read selected | |
chosen="${versions[$selected - 1]}" | |
else | |
echo "" | |
echo 'Installed PHP versions:' | |
for i in "${versions[@]}"; do | |
if [ $1 == $i ]; then | |
echo -e "$(($counter + 1))) PHP $i ${color_green}<- activating...${color_off}" | |
chosen="${versions[$counter]}" | |
else | |
echo "$(($counter + 1))) PHP $i" | |
fi | |
counter=$(($counter + 1)) | |
done | |
fi | |
if [ -z "$chosen" ]; then | |
echo "The selected PHP version is not installed!" | |
exit 1 | |
fi | |
for i in "${versions[@]}"; do | |
sudo a2dismod php$i &>/dev/null | |
done | |
echo "" | |
echo -e "${color_green}Activating PHP $chosen...${color_off}" | |
echo "" | |
sudo a2enmod php$chosen | |
sudo service apache2 restart | |
sudo ln -sfn /usr/bin/php$chosen /etc/alternatives/php | |
echo "" | |
echo -e "${color_green}PHP $chosen is now active!${color_off}" |
The script can now run with a parameter (eg. $ phpswitch 7.4
) or without parameters (eg. $ phpswitch
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what it looks like: