In a Shell scripts, check version of installed package, make a decision based on output

Posted by DJDarkViper on Super User See other posts from Super User or by DJDarkViper
Published on 2013-10-24T21:28:49Z Indexed on 2013/10/24 21:58 UTC
Read the original article Hit count: 264

Filed under:
|
|
|
|

Looking to write a cross distro / cross version shell script that makes sure a forced version of PHP is installed

Example: Ubuntu 12.04 has 5.3, Ubuntu 13.10 has 5.5, Debian 7 has 5.4

I need this script, when run on a distro that has an old version of PHP, to update the repo to point to a package for 5.4, and if the distro has too new of a version, can downgrade to 5.4 appropriately.

Im still not entirely comprehensive of Shell/Terminals technical limit of what you can do with it, but ill be perfectly frank that im still not totally used to existing tools

The best I can think at the moment is: php -v | grep "PHP 5" but that returns a bunch of potentially changeable granular characters (PHP 5.4.4-14+deb7u5 (cli) (built: Oct 3 2013 09:24:58) ). Im not sure what to pipe to after this to extract out the characters im interested in

Im not sure if im being totally clear, im not sure how to ask this.. Basically, in an automated shell script for Linux distros, how do I extract the PHP version (and just the PHP version number preferably) and make a decision based on that output

EDIT

This line ended up doing pretty dang good

php -v | grep "PHP 5" | sed 's/.*PHP \([^-]*\).*/\1/' | cut -c 1-3

Bit long in the tooth, but gives me "5.3", "5.4", and "5.5" which is exactly what I need to work with

© Super User or respective owner

Related posts about ubuntu

Related posts about terminal