?
Solved

detecting version of linux

Posted on 2003-03-17
5
Medium Priority
?
2,261 Views
Last Modified: 2013-12-06
We have a shell script that currently asks if the user is running Debian, Red Hat, or SUSE, and adapts a makefile accordingly. (I think Red Hat may be the oddman out.)  We'd like to determine what distribution of Linux they're running.  

A short and acceptable (and wonderful) answer might be a snippet of shell script (including the #! at the top) that should run on all those flavors of Linux, and do the follow pseudocode: (I think the existing script runs under ksh and bash? Does Linux not usually come with ksh?)

IF INSTALL=REDHAT
  echo redhat
END IF
IF INSTALL=SUSE
  echo suse
END IF
IF INSTALL=DEBIAN
  echo debian
END IF

I'm not sure what the most reliable way of detecting dist is; though gcc -v seems to work.  But then I'm not sure how to redirect it's stderr to stdout under ksh and/or bash, and my shell scripting is weak enough that I don't know the easiest way of checking the result of that grep, so any advice on that would be appreciated as well...

Thanks!
 
0
Comment
Question by:kisrael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Accepted Solution

by:
bummerlord earned 900 total points
ID: 8156659
Why not use methods that will work regardless of distribution.

A few hints;
1) Use Bourn shell (#!/bin/sh) (even though some systems have /bin/sh being a link to /bin/bash, bash will then act in 'sh' compability mode)
Properly written your script should/will work not only in Linux but in Solaris, AIX, QNX, *BSD, SGI etc etc

2) The dependencies of what you attempt to install can be checked within your script regardless of dirstribution (don't assume users have left the system "intact" after installation...)
A true Linux user have most likely choosen Linux for this reason. To be able to tailor it to hell(tm), and they will ;-)

3) Don't assume anything, test for everything (or the user (assume they are more experienced than you are) will get back at you with a vengeance if you trash up their system.)

To answer your script related questions;
(think /bin/sh from now on!)

Redirecting stderr to stdout you can usually do:
command 2>&1 | grep ..

example:
unix# gcc -v 2>&1 | awk '{  s=split($4,r,"/"); for (n=1;n<=s;n++) if ( r[n] ~ /[l|L]inux/) print r[n] ; exit}'


There is no single way to detect distribution reliably. Asking the user may be the best option if you really must know what distribution was used during install.
Though the distribution may not be as interesting as the  kernel version, glibc and other libraries existance/versions... depending on what you attempt to achive of course.

I think the RedHat installation leaves a file behind (/etc/redhat-release) that contains distribution name and version.

/b
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8157287
Yes, some distributions do leave those files there.  Examples:

/etc/redhat-release
/etc/debian_version
/etc/slackware-version

But in concurrence with bummerlord, I certainly wouldn't rely on this.
0
 

Expert Comment

by:szaboc
ID: 8165614
On RedHat you can do "cat /proc/version"; don't know 'bout the others
0
 
LVL 6

Expert Comment

by:bummerlord
ID: 8170148
szaboc, that has nothing to do with distributions and should be the same for all distributions with a reasonably recent kernel.
As you know the proc filesystem is a virtual filesystem provided by Linux (Linux is really only the kernel) as a link to read/alter some parameters, either by humans or user space programs.

/b
0
 

Expert Comment

by:cosie
ID: 32817471
/usr/bin/lsb_release -a
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question