forked from rapidwebltd/php-switch-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch-php.sh
executable file
·70 lines (59 loc) · 2.24 KB
/
switch-php.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
function switch_php() {
local prompt="$1" outvar="$2"
shift
shift
local options=("$@") cur=0 count=${#options[@]} index=0
local esc=$(echo -en "\e") # cache ESC as test doesn't allow esc codes
printf "$prompt\n"
while true
do
# list all options (option list is zero-based)
index=0
for o in "${options[@]}"
do
if [ "$index" == "$cur" ]
then
echo -e " >\e[7m$o\e[0m" # mark & highlight the current option
else
echo " $o"
fi
index=$(( $index + 1 ))
done
read -s -n3 key # wait for user to key in arrows or ENTER
if [[ $key == $esc[A ]] # up arrow
then cur=$(( $cur - 1 ))
[ "$cur" -lt 0 ] && cur=0
elif [[ $key == $esc[B ]] # down arrow
then cur=$(( $cur + 1 ))
[ "$cur" -ge $count ] && cur=$(( $count - 1 ))
elif [[ $key == "" ]] # nothing, i.e the read delimiter - ENTER
then break
fi
echo -en "\e[${count}A" # go up to the beginning to re-render
done
printf -v $outvar "${options[$cur]}"
if [[ $cur -gt 0 ]]
then
echo "* Enabling Apache PHP ${options[$cur]} module"
sudo a2enconf php${options[$cur]}-fpm
echo "* Disabling Apache PHP $CURRENT_PHP_VERSION module"
sudo a2disconf php$CURRENT_PHP_VERSION-fpm
echo "* Restarting Apache..."
sudo service apache2 restart
echo "* Switching CLI PHP to ${options[$cur]}"
sudo update-alternatives --set php /usr/bin/php${options[$cur]} > /dev/null
echo "* Current PHP-version: $(php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d".")"
fi
}
CURRENT_PHP_VERSION=$(php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d"." )
AVAILABLE_PHP_VERSIONS=$(ls /usr/bin/php* | grep -oP 'php([0-9]+\.[0-9]+)' | sed 's/php//g' | sort -rV | uniq)
options=("Cancel, stay on PHP $CURRENT_PHP_VERSION")
# Filter out the current PHP version because switching to the same version gives unexpected results
for version in $AVAILABLE_PHP_VERSIONS
do
if [ "$version" != "$CURRENT_PHP_VERSION" ]; then
options+=("$version")
fi
done
switch_php "Switch to PHP-version:" selected_choice "${options[@]}"