-
-
Save SpekkoRice/694e4e33ee298361b642 to your computer and use it in GitHub Desktop.
#!/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 | |
} |
It doesn't work for anything but PHP 5 because it's hardcoded to look for versions starting with 5. However, switching the 5 out for a generic digit matches operating system information so you have to narrow the search to things preceeded by "PHP ". Then you have to trim them PHP part out of the result.
# The current PHP Version of the machine
PHPVersion=$(php -v|grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+");
echo $PHPVersion
# Truncate the string abit so we can do a binary comparison
currentVersion=${PHPVersion:4:3};
It doesn't work for anything but PHP 5 because it's hardcoded to look for versions starting with 5. However, switching the 5 out for a generic digit matches operating system information so you have to narrow the search to things preceeded by "PHP ". Then you have to trim them PHP part out of the result.
# The current PHP Version of the machine
PHPVersion=$(php -v|grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+");
echo $PHPVersion
# Truncate the string abit so we can do a binary comparison
currentVersion=${PHPVersion:4:3};
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
Core script is not compatible with php versions greater than 5, apparently.