Recent

Author Topic: [Solved] Differentiate between various Linux distros  (Read 2587 times)

petevick

  • Sr. Member
  • ****
  • Posts: 347
[Solved] Differentiate between various Linux distros
« on: May 29, 2022, 04:56:59 pm »
At the moment in my project I differentiate between Linux and Windows by this code....
Code: Pascal  [Select][+][-]
  1. { TForm1 }
  2. function OSVersion: String;
  3. begin
  4.   {$IFDEF Linux}
  5.   OSVersion := 'Linux';
  6.   {$ELSE}
  7.   {$IFDEF WINDOWS}
  8.   OSVersion := 'Windows';
  9.   {$ENDIF}
  10.   {$ENDIF}
  11. end;
  12.  
...but is there a way to differentiate between other Linux/Ubuntu distros, say Linux Mint and Kubuntu.
« Last Edit: May 30, 2022, 10:01:11 am by petevick »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 347
Re: Differentiate between various Linux distros
« Reply #1 on: May 29, 2022, 05:44:27 pm »
Hmmm, maybe I just expand on what I have, something like.....

Code: Pascal  [Select][+][-]
  1. { TForm1 }
  2. function OSVersion: String;
  3. begin
  4.   {$IFDEF Linux}
  5.   OSVersion := 'Linux';
  6.   {$ELSE}
  7.   {$IFDEF Ubuntu}
  8.   OSVersion := 'Ubuntu';
  9.   {$ELSE}
  10.   {$IFDEF WINDOWS}
  11.   OSVersion := 'Windows';
  12.   {$ENDIF}
  13.   {$ENDIF}
  14.   {$ENDIF}
  15. end;

« Last Edit: May 29, 2022, 05:49:05 pm by petevick »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 347
Re: Differentiate between various Linux distros
« Reply #2 on: May 29, 2022, 05:56:07 pm »
Nope, that doesn't work  %)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Differentiate between various Linux distros
« Reply #3 on: May 29, 2022, 06:29:37 pm »
Have a look at /etc/os-release, there is also a man page (man os-release).

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Differentiate between various Linux distros
« Reply #4 on: May 29, 2022, 06:39:24 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   FileUtil;
  3.  
  4. function GetLinuxDistro: String;
  5. const
  6.   Issue = '/etc/issue';
  7. begin
  8.   case FileExists(Issue) of
  9.     True:  Result := ReadFileToString(Issue);
  10.     False: Result := 'unknown distro';
  11.   end;
  12. end;
       

petevick

  • Sr. Member
  • ****
  • Posts: 347
Re: Differentiate between various Linux distros
« Reply #5 on: May 29, 2022, 06:49:22 pm »
Have a look at /etc/os-release, there is also a man page (man os-release).
Thanks for this Lutz, but I think the solution from howardpc will be a tad more straight forward as 'issue' is just a one line text file.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 347
Re: Differentiate between various Linux distros
« Reply #6 on: May 29, 2022, 06:50:49 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   FileUtil;
  3.  
  4. function GetLinuxDistro: String;
  5. const
  6.   Issue = '/etc/issue';
  7. begin
  8.   case FileExists(Issue) of
  9.     True:  Result := ReadFileToString(Issue);
  10.     False: Result := 'unknown distro';
  11.   end;
  12. end;
     
Thanks for this, It should work for me.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

Kays

  • Hero Member
  • *****
  • Posts: 576
  • Whasup!?
    • KaiBurghardt.de
Re: Differentiate between various Linux distros
« Reply #7 on: May 29, 2022, 07:05:45 pm »
Have a look at /etc/os-release, there is also a man page (man os-release).
Right. Everything’s runnin’ on systemd nowadays.
Code: Pascal  [Select][+][-]
  1.   OSVersion := 'systemd';
Yours Sincerely
Kai Burghardt

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Differentiate between various Linux distros
« Reply #8 on: May 29, 2022, 07:15:32 pm »
Have a look at /etc/os-release, there is also a man page (man os-release).
Thanks for this Lutz, but I think the solution from howardpc will be a tad more straight forward as 'issue' is just a one line text file.

I believe /etc/os-release is the nearest thing you'll get to a correct answer since this is officially sanctioned by both linux.org and freedesktop.org: hence you will also find it supported by Solaris etc., with standardised content.

Anything using conditional compilation is doomed to failure, since all you're doing is asking the build environment what it thinks it's compiling for. So as a specific example, you might be building a 32-bit Linux program with no GUI which is going to be run on a 64-bit distro with KDE.

Conditionals do have their uses, but what you're trying to do isn't one of them :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Differentiate between various Linux distros
« Reply #9 on: May 29, 2022, 09:39:13 pm »
Have a look at /etc/os-release, there is also a man page (man os-release).
Right. Everything’s runnin’ on systemd nowadays.
Code: Pascal  [Select][+][-]
  1.   OSVersion := 'systemd';

Hi!

This is not correct.

There is a hardcore crew that is angry about systemd.

Have a look at

https://nosystemd.org/

Winni

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: Differentiate between various Linux distros
« Reply #10 on: May 29, 2022, 11:55:18 pm »
Hey Y'all,

This actually made me curious of how many ways we can assess this and I've added another app to my Test* collection of apps: Test Linux Distribution Information

Of course I went all overboard and it even has i18n with one language: Portuguese.

Hope it's useful for anyone.

TODO:
  • Get the information via the lsb_release CLI command
  • Find if these 4/5 methods are really the whole shibang or if there are others.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Differentiate between various Linux distros
« Reply #11 on: May 30, 2022, 08:25:18 am »

This is not correct.

There is a hardcore crew that is angry about systemd.
Fake news? For sure. There is a very valid reason everybody is using systemd: it works better than the old alternatives.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

friend

  • Guest
Re: Differentiate between various Linux distros
« Reply #12 on: May 30, 2022, 09:55:37 am »
Fake news? For sure. There is a very valid reason everybody is using systemd: it works better than the old alternatives.

You don't know what you're talking about. This cannot even be called "news" -- now the association is unsurprising for someone whose profile picture is a flag of Ukraine and recently called the President of the Russian Federation a criminal in his signature. You seem to like talking about what you don't know.
I bet you haven't even read about the reasons why systemd is criticized.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Differentiate between various Linux distros
« Reply #13 on: May 30, 2022, 10:51:51 am »

This is not correct.

There is a hardcore crew that is angry about systemd.
Fake news? For sure. There is a very valid reason everybody is using systemd: it works better than the old alternatives.


Don't call everything you're not informed about "fake news".

Some former Debian Developers issued an non-systemd Debian called Devuan:

https://www.devuan.org/

A (not complete) list of Linux distros without systemd:

https://en.wikipedia.org/wiki/Category:Linux_distributions_without_systemd

Winni

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: [Solved] Differentiate between various Linux distros
« Reply #14 on: May 30, 2022, 10:52:58 am »
Some people are also still maintaining Minix. That doesn't mean Linux is irrelevant, but also not that all Linux code should still work with Minix.

 

TinyPortal © 2005-2018