Last active
January 24, 2020 17:28
-
-
Save judgej/318b59b1e57867ffd2968dfa1521421a to your computer and use it in GitHub Desktop.
bash script to switch between PHP versions in Plesk
This file contains hidden or 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 | |
# Use the script like this: | |
# . setphp 7.1 | |
# or | |
# source setphp 5.6 | |
# | |
# It will look for the path to the current PHP version in your PATH and switch to | |
# the new PHP version. | |
# If you do not have a path to a php version, then it will add one. | |
# If the version you specify does not exist, it will list the versions that are available. | |
# I wrote this for a Plesk server with various applications running under different versions | |
# of PHP, allowing me to quickly swicth between them on the command line. | |
# https://stackoverflow.com/questions/8063228/how-do-i-check-if-a-variable-exists-in-a-list-in-bash | |
# list_include_item "10 11 12" "2" | |
function list_include_item { | |
local list="$1" | |
local item="$2" | |
if [[ $list =~ (^|[[:space:]])"$item"($|[[:space:]]) ]] ; then | |
# yes, list include item | |
result=0 | |
else | |
result=1 | |
fi | |
return $result | |
} | |
wrapper_func() { | |
# The location of the PHP versions supplied by Plesk. | |
local BASEDIR="/opt/plesk/php/" | |
# The requested PHP version. | |
local VERSION=$1 | |
# The available PHP versions. | |
local VERSIONS=$(ls $BASEDIR) | |
if $(list_include_item "$VERSIONS" "$VERSION") ; then | |
echo $PATH | |
if [[ $PATH = *"$BASEDIR"* ]]; then | |
PATH=$(echo $PATH | sed 's#'$BASEDIR'[0-9.]*[^/]/bin#'$BASEDIR$VERSION'/bin#') | |
echo "Updated:" | |
else | |
echo "Added:" | |
PATH="$BASEDIR$VERSION/bin:$PATH" | |
fi | |
echo $PATH | |
else | |
echo Usage: $(basename $0) php-version | |
echo Versions are: $VERSIONS | |
fi | |
} | |
wrapper_func $1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't open PR on this Gist.