1

I run a cron job (perl script) which fetches the PHP-version from servers and stores them in a database. This has always worked with CentOS PHP versions. I had to modify it a little in order to make it work with Debian. So far all is good. However it's getting the wrong version / information with Ubtuntu PHP.

This is the code snippet:

@temp = `/usr/sbin/vzctl exec $server 'php -v 2>/dev/null | /bin/grep "built\\|cgi\\|cli" 2>&1'`;
chomp(@temp);
@temp = split /\s+/,$temp[0];
$php = $temp[1];
$php =~ s/^.*(\d+\.\d+\.\d+).*$/$1/;

Below are 3 examples from different PHP-versions on different operating systems:

CentOS 7

php -v outputs: PHP 7.0.33 (cli) (built: Oct 22 2019 10:24:41) ( NTS )
Result in database: 7.0.33 (= correct)

Debian 9

php -v outputs: PHP 7.3.10-1+0~20191008.45+debian9~1.gbp365209 (cli) (built: Oct  8 2019 05:48:14) ( NTS )
Result in database: 7.3.10 (= correct)

Ubuntu 18.04

php -v outputs: PHP 7.2.19-0ubuntu0.18.04.2 (cli) (built: Aug 12 2019 19:34:28) ( NTS )
Result in database: 8.04.2 (= incorrect)

I think the current regex expression is considering the last part as the PHP version, hence it's storing 8.04.2 in the database.

How can the current code:

$php =~ s/^.*(\d+\.\d+\.\d+).*$/$1/;

Be modified so it reads the correct PHP version for all operating systems?

I checked a few older servers with dates operating systems, but they don't differ much from the above 3 examples. So I am guessing if the current code can be tweaked to also work with Ubuntu, I am set for now and the near future.

Please keep in mind that I want to be able to use the current code (as displayed above) but with a modified regex obviously.

So two questions (both also for learning):

  1. Is it possible to adjust the current regex to match PHP versions for all OS as shown above?
  2. isn't it better to make the regex search the text PHP and then grab the version somehow?

I know the code is old, but it's from our technician who unfortunately is not amongst us anymore.

Maybe someone cares to take a look and come up with a working solution based on it's current form?

Thanks!

Joanne
  • 514
  • 3
  • 8
  • 30
  • 3
    Make the first quantifier reluctant (non-greedy). Adding `"PHP "` before is also a good idea. – Casimir et Hippolyte Oct 25 '19 at 11:23
  • If you simplify the regex, it seems to work - `/(\d+\.\d+\.\d+)/`. – Dave Cross Oct 25 '19 at 12:02
  • I don't understand why you thought you needed `^.*` at the start of your regex and `.*$` at the end. You don't need to match your whole string - just look for the bits you're interested in. – Dave Cross Oct 25 '19 at 12:28
  • 1
    Try this $php =~ s/^.*PHP (\d+\.\d+\.\d+).*$/$1/; since php is common starting point – Srihari Karanth Oct 25 '19 at 15:35
  • Thank you Srihari for your answer. I tried your solution, which works as for displaying the PHP-version. But... It also adds everything behind, for example: 7.2.19-0ubuntu0.18.0 (Ubuntu server) and 7.3.10-1+0~20191008 (Debian server). Is there a way to resolve this also? – Joanne Oct 28 '19 at 12:42
  • @Srihari Karanth forgot to use @. – Joanne Oct 29 '19 at 07:42
  • 1
    @Joanne, thats not possible as the content in bracket only has dot and digits. It cant show - in it. Are you sure you have used it correctly? Can you paste what you have used? – Srihari Karanth Oct 29 '19 at 17:15
  • @Srihari Karanth, I only changed the last line to: $php =~ s/^.*PHP (\d+\.\d+\.\d+).*$/$1/; – Joanne Oct 30 '19 at 11:05
  • @Srihari Karanth no clue but it's not working. But I will leave it like this I guess. No clue why it's taking the Ubuntu information into it. – Joanne Nov 06 '19 at 12:19
  • Can you paste the whole code after update ? You can update your question with the new code maybe. – Srihari Karanth Nov 06 '19 at 12:25
  • I tested your above code and it works perfectly. Looks like you are not using the variable properly? You might be using the wrong variable for display. – Srihari Karanth Nov 06 '19 at 12:33
  • I will check it once again and redo it completely. Maybe a typo somewhere (which I doubt) or the wrong variable indeed, as you say. – Joanne Nov 06 '19 at 13:15
  • @Srihari Karanth I finally found the issue!! Apparently there are 2 similar scripts which do the same action (getting PHP information and such). I only modified one! I discovered this just now. I replaced it also in the other script and now it's working correctly! Thank you again!! – Joanne Nov 12 '19 at 07:58

0 Answers0