Linux distro name
I want to get linux distribution name. I googled and found lsb_release :)
Does it a standard way? Does it installed on linux distributions as default?
Is there any library in C or C++ to use that or I have to use system(); ?
And is there any other way?
Thanks for your consideration
Maybe I had to mention this:
There are /etc/debian_version and /etc/issue files in debian based linux.
But any standard way to do this in all linux?
uname is the command.
But whether there is a system call behind that, I've no idea.
/etc/issue is probably on most systems.
The distro is not that relevant to C programming since all distos are GNU based.
If you are doing something a certain way and are not sure if it will work on all distros, rather than check the distro just test your method (eg, are the files you presume present, present?). In other words, aim to make it work where ever possible, and fail gracefully where it will not.
The other issue is that just because a system is based on a given distro does not mean you can rely on it having certain characteristics. A lot can be modified; if you presume that the system is a certain way just because it is "debian", you will easily get caught with your pants down. You must test for the characteristics themselves.
Thank you MK27 :)
My program is a service and I should add it to system services and it is distro dependant!!!
For debian based, redhat based and openSuse !!!!
I thought that the best way to do that is to get distro name and do the compatible commands for each of them!!!!
Is there any other way (or standard way to do this on all Linux)?
Then test the presence of, say, /etc/init.d /etc/rcx.d, etc.
I believe that's what MK27 meant by testing for characteristics.
I wrote this function :( It's not beauty!!! but it can work on most conditions, of course not all. Add it or improve it please. Hope that we can write something useful, other can use and enjoy :)
( I categorized Linux distributions (as my need) in: REDHAT, SUSE and DEBIAN based )
//0 redhat, 1 suse, 2 debian, 3 NONE
system("cat /etc/issue > .distro");
if( access("/etc/redhat-release",F_OK)==0 || strcmp(distro,"redhat")==0 || strcmp(distro,"centos")==0
|| strcmp(distro,"fedora")==0 ) return 0;
else if( access("/etc/suse_release",F_OK)==0 || strcmp(distro,"opensuse")==0 || strcmp(distro,"suse")==0 )
else if( access("/etc/debian_version",F_OK)==0 || strcmp(distro,"ubuntu")==0 || strcmp(distro,"debian")==0 )
system("lsb_release -si > .distro");
if( strcmp(distro,"centos")==0 || strcmp(distro,"redhat")==0 || strcmp(distro,"fedora")==0 )
else if( strcmp(distro,"opensuse")==0 || strcmp(distro,"suse")==0 )
else if( strcmp(distro,"ubuntu")==0 || strcmp(distro,"debian")==0 )
There are many similar distros that probably have similar directory structures. On the Redhat side, there's SuSE, Mandriva, CentOS, Fedora... and on the Debian side, there's Ubuntu (and it's various variants - kubuntu, xubuntu, edubuntu, etc), knoppix, Xandros, Linspire (or whatever it's called now).... Those are just the ones I can name. There are MANY MANY others. That's why we have been suggesting that it is impractical to test for distributions, and it would be a better idea to test for specific features you need.
I said that this is according to my need. I just need to 3 sections I mentioned, everyone can add to this function and use more.
Yes, but as I warned you these things can be modified. So if you test for the distro and someone has modified the filesystem structure, which could easily be the case -- for example, some available server set-ups may be different than the desktop set-up and these differences can be more or less extensive -- then what you *think* is supposed to be true just because a system is "redhat" is NOT NECESSARILY SO.
Originally Posted by hosseinyounesi
YOU MUST TEST SPECIFICALLY FOR THE FILES AND OTHER RESOURCES YOU WANT TO WORK WITH. JUST SAYING "OH, LOOK, IT'S DEBIAN" IS A VERY BAD IDEA AND BOUND TO LEAD TO PROBLEMS.
If you intention is to distribute this or have it used on other computers, anyone with half a brain will take one look at that method, know it for what it is (a very poor hack) and simply not use the software. If you are just writing something for use by yourself, or in an environment like an office where you will always be the one doing the maintenance, *you* might get away with it, but as I said it will not fly further than that this way.
Plus, you are then saying "oh if another distro could look like this we could add more functions". Again, you are obviously trying to find ways to compensate for *your* laziness by proposing work-arounds for the consequences.
Learn to use the system properly. It may take you some more time, but it will mean the difference between something that works (properly) and something that works via a series of fortuitous coincidences based on "well it worked for me that one time" etc. Which the last one is quite likely to not work at all when production tested.
> My program is a service and I should add it to system services and it is distro dependant!!!
I would suggest you find the source / installer for other common services, and see how they manage it.
As MK27 says, some half-baked detection of the current OS is going to come unstuck, if there is a more generally accepted way of solving the problem.
Thank you MK27 :) You are right, but I thought that at least "lsb_release" will work!!!
This program works in my office, but now I'm going to search more and read some services code to detect the way!!!!
But I have to say this too:
most of programs and distributions are in .deb, .rpm and tar.gz files. I think with these extensions the distribution is chosen by the user itself?!
Thanks again & again