Skip to content

Instantly share code, notes, and snippets.

@josuealcalde
Forked from elipettingale/.bash_aliases
Created July 21, 2022 08:13
Show Gist options
  • Select an option

  • Save josuealcalde/e10a9e8dc9018ba9c60b7c806995c1c6 to your computer and use it in GitHub Desktop.

Select an option

Save josuealcalde/e10a9e8dc9018ba9c60b7c806995c1c6 to your computer and use it in GitHub Desktop.
PHP Version Manager - quickly switch terminal to a specific version of php
#!/bin/bash
PVM_TMP_PATH=$(mktemp -d)
RETVAL=0
export PATH="$PVM_TMP_PATH:$PATH"
function pvm_use() {
if [ -f "/usr/bin/php${1}" ]
then
printf "Now using php $1\n"
rm -f $PVM_TMP_PATH/php
rm -f $PVM_TMP_PATH/php-config
rm -f $PVM_TMP_PATH/phpize
ln -s /usr/bin/php$1 $PVM_TMP_PATH/php
ln -s /usr/bin/php-config$1 $PVM_TMP_PATH/php-config
ln -s /usr/bin/phpize$1 $PVM_TMP_PATH/phpize
hash -d php > /dev/null 2> /dev/null
hash -d php-config > /dev/null 2> /dev/null
hash -d phpize > /dev/null 2> /dev/null
else
printf "\033[1;31mVersion not found at /usr/bin/php${1}\033[0m\n"
exit 1
fi
}
if [ $1 = "use" ]
then
if [ -z "$2" ]
then
if [ -f ".php-version" ]
then
VERSION=$(cat .php-version)
pvm_use "$VERSION"
else
printf "\033[1;31mNo .php-version file found\033[0m\n"
exit 2
fi
elif [ $2 = "latest" ]
then
LATEST=$(find /usr/bin -regextype sed -regex "/usr/bin/php[0-9].[0-9]" | sort -n | tail -n 1)
LATEST=${LATEST:12}
pvm_use "$LATEST"
else
pvm_use "$2"
fi
fi
if [ $1 = "list" ]
then
VERSIONS=$(find /usr/bin -regextype sed -regex "/usr/bin/php[0-9].[0-9]" | sort -n)
VERSIONS=($VERSIONS)
CURRENT_LONG=$(php -v | head -n 1 | cut -d " " -f 2)
CURRENT=$(echo $CURRENT_LONG | cut -c 1-3)
for i in "${VERSIONS[@]}"
do
if [ ${i:12} = $CURRENT ]
then
printf "\033[1;32m-> ${CURRENT} (${CURRENT_LONG})\033[0m\n"
else
echo "${i:12}"
fi
done
fi
if [ $1 = "-v" ]
then
CURRENT=$(php -v | head -n 1 | cut -d " " -f 2 | cut -c 1-3)
echo $CURRENT
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment