-
-
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 | |
} |
php --version | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1,3
# for php71 output is 71
useful for such things as
phpversion="$(php --version | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1,3)"
brew unlink php$phpversion
For systems lacking of tail -r
, this should also work:
php --version | head -n 1 | cut -d " " -f 2 | cut -c 1,3
php -r "echo PHP_VERSION;"
???? Simple ??
Thanks for this! Tweaked it some to meet my needs. I don't know much about shell scripts, but I am getting there.
export PHP_VERSION=$(php -r "echo PHP_VERSION;" | grep --only-matching --perl-regexp "7.\d+")
Core script is not compatible with php versions greater than 5, apparently.
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
When your PHP version is something like
5.4.16
then your truncate will produce5.4.
string and function will return false. IMHO better (but not ideal) truncate is: