#!/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