skip to Main Content

I am trying to display the version number of a Linux distro on a web page. The distro is DietPi. The version number is stored in /DietPi/dietpi/.version and looks like this;

G_DIETPI_VERSION_CORE=7
G_DIETPI_VERSION_SUB=3
G_DIETPI_VERSION_RC=2
G_GITBRANCH='master'
G_GITOWNER='MichaIng'

This is what I have experimented with so far, which I’ve put together from examples I have found on SO.

<?php system("cat /DietPi/dietpi/.version  | cut -f2 -d'=' | sed 's/[^0-9]*//g' ") ;?>

This works but there are spaces between the digits thus
7 3 2
and I need to add periods so it looks like this
7.3.2

I suspect the above is probably not the most efficient way of doing this. I’d welcome help on inserting the periods or ideas on simpler methods that would work for me.

4

Answers


  1. A simple way to do it, using what you have done already, is to just replace the spaces with dots. You can use str_replace for that.

    Login or Signup to reply.
  2. The laziest option would be parse_ini_file and extract. (The usual caveat: do not use with arbitrary input. Seems perfectly reasonable in this case.)

    extract(parse_ini_file("/DietPi/dietpi/.version"));
    

    Then you have all entries available as vars for string concatenation right away:

    echo "$G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC";
    

    If there are e.g. stray carriage returns, then applying trim might still be necessary. – Do an array_map("trim", …) before the extract().
    Typically parse_ini would already strip off trailing whitespace however.

    Login or Signup to reply.
  3. @mario did beat me to it, but I had the same idea, this is my suggestion:

    $ver = parse_ini_file('/DietPi/dietpi/.version');
    
    $full = sprintf('%d.%d.%d', $ver['G_DIETPI_VERSION_CORE'], $ver['G_DIETPI_VERSION_SUB'], $ver['G_DIETPI_VERSION_RC']);
    
    var_dump($full); // string(5) "7.3.2"
    
    • uses an array instead of extract
    • sprintf to normalize the string to integer values

    There is a shortcut that can be taken in this case as those are the first three values in that file:

    $ver = parse_ini_file('/DietPi/dietpi/.version');
    
    $full = vsprintf('%d.%d.%d', $ver); // string(5) "7.3.2"
    

    your mileage my vary, its basically the same, I think the main benefit is using parse_ini_file over system.

    $full = vsprintf('%d.%d.%d', parse_ini_file('/DietPi/dietpi/.version'));
    // string(5) "7.3.2"
    

    (you may want to throw exceptions on warnings with this last variant to at least deal with problems getting the version number)

    Login or Signup to reply.
  4. I believe DietPi already sources these values to your environment. If that’s the case, you can skip the file read/parse and just pull directly out of $_ENV. (I’m not 100% sure about this, but it’ll be a quick test for you at least.)

    $version = sprintf(
        '%s.%s.%s',
        $_ENV['G_DIETPI_VERSION_CORE'],
        $_ENV['G_DIETPI_VERSION_SUB'],
        $_ENV['G_DIETPI_VERSION_RC']
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search