Last active
July 21, 2020 07:40
-
-
Save SpekkoRice/694e4e33ee298361b642 to your computer and use it in GitHub Desktop.
Bash: Check the version of PHP
This file contains 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 | |
# Function to check if the PHP version is valid | |
checkPHPVersion() { | |
# The current PHP Version of the machine | |
PHPVersion=$(php -v|grep --only-matching --perl-regexp "5\.\\d+\.\\d+"); | |
# Truncate the string abit so we can do a binary comparison | |
currentVersion=${PHPVersion::0-2}; | |
# The version to validate against | |
minimumRequiredVersion=$1; | |
# If the version match | |
if [ $(echo " $currentVersion >= $minimumRequiredVersion" | bc) -eq 1 ]; then | |
# Notify that the versions are matching | |
echo "PHP Version is valid ..."; | |
else | |
# Else notify that the version are not matching | |
echo "PHP Version NOT valid for ${currentVersion} ..."; | |
# Kill the script | |
exit 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the tip. I have modified it to use
cut
so that I can use it in a shell script. The final result is:php -v|grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+"|cut -c 5-7
And I used it to create the following shell script:
https://gist.github.com/haljeshi/329a0fb0c6df43f3fc904d716a96af1b