-
-
Save nhoag/33504e22c922d9b035c6d7be32f8f0ac to your computer and use it in GitHub Desktop.
PHP switcher
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 | |
# Check if command was run as root. | |
if [[ $(id -u) -eq 0 ]]; then | |
echo "The command \"sphp\" should not be executed as root or via sudo directly." | |
echo "When a service requires root access, you will be prompted for a password as needed." | |
exit 1 | |
fi | |
# Usage | |
if [[ $# -ne 1 ]]; then | |
echo "Usage: sphp [phpversion]" | |
echo "Versions installed:" | |
brew list | grep -E '^php(\@|$)' | while read -r line ; do | |
echo " - phpversion: $line" | |
done | |
exit 1 | |
fi | |
currentVersion="php@$(php -r "error_reporting(0); echo substr(phpversion(), 0, 3);")" | |
newVersion="$1" | |
if ! brew list "${newVersion}" &> /dev/null; then | |
echo "PHP version $newVersion was not found." | |
echo "Try \`brew install ${newVersion}\` first." | |
exit 1 | |
fi | |
if [[ "${currentVersion}" == "$newVersion" ]]; then | |
echo "PHP version $newVersion is already active." | |
exit 1 | |
fi | |
echo "PHP version $newVersion found" | |
echo "Unlinking old binaries..." | |
brew unlink "${currentVersion}" &> /dev/null | |
echo "Linking new binaries..." | |
brew link --force --overwrite "${newVersion}" &> /dev/null | |
echo "Done." | |
# Show PHP CLI version for verification. | |
echo && php -v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pulled out Apache stuff (only use it virtually), deleted commented out code, and ran what was left through
shellcheck
.