Skip to content

Instantly share code, notes, and snippets.

@nhoag
Forked from w00fz/sphp.sh
Last active May 7, 2019 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhoag/33504e22c922d9b035c6d7be32f8f0ac to your computer and use it in GitHub Desktop.
Save nhoag/33504e22c922d9b035c6d7be32f8f0ac to your computer and use it in GitHub Desktop.
PHP switcher
#!/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
@nhoag
Copy link
Author

nhoag commented Dec 12, 2017

Pulled out Apache stuff (only use it virtually), deleted commented out code, and ran what was left through shellcheck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment