simple script detect to find OS version/flavour

Hi,

A newbie question.

Following script gives no output.

root@srv [~]# cat /etc/redhat-release | awk {'print $1}'
Red

root@srv [~]# cat 123.sh
if (( `cat /etc/redhat-release | awk {'print $1}'` != CentOS )); then { echo "System runs on Redhat Linux. Exiting..."; exit; } fi

root@srv [~]# sh 123.sh
root@srv [~]#

root@srv [~]# if (( `cat /etc/redhat-release | awk {'print $1}'` != CentOS )); then { echo "System runs on Redhat Linux. Exiting..."; exit; } fi
root@srv [~]#

root@srv [~]# sh -x 123.sh
++ cat /etc/redhat-release
++ awk '{print $1}'
+ (( Red != CentOS ))

root@srv [~]#

Please advise or provide a alternative so that I can include it in a shell script that will use up2date, in case it is RHEL ( i am aware RHEL 5 use yum ) and will use yum incase it is CentOS

Thanks

OS=`cat /etc/redhat-release | awk {'print $1}'`
if [ "$OS" != "CentOS" ]
then
echo "System runs on Redhat Linux. Exiting...";
exit;
fi

Does CentOS have the /etc/redhat-release file?

Just test for the existence of the file

if [[ -f /etc/redhat-release ]] ; then
  OS=redhat
else
  OS=CentOS
fi

Hi,

CentOS does have /etc/redhat-release file

Thanks palsevlohit_123 , it works. Lets extend script abit.

Updating in a moment

If /etc/redhat-release doesnot exit , echo system not running redhat or CentOS or redhat-release file is missing, then do nothing below, if /etc/redhat-release exists , then find the OS flavour and,

## if system is Redhat
OS=`cat /etc/redhat-release | awk {'print $1}'`
if [ "$OS" != "CentOS" ]
then
echo "System runs on Redhat Linux.";

do
up2date -i ncurses dialog nmap
done
exit;
fi

## if system is Centos
echo "System runs on CentOS.";
do
yum -y install ncurses dialog nmap
done

I can run the above script two times and one of them will do the install, yum or up2date. I will look better , if we use the value ( Red or CentOS ) and then build a script like ( nested or whatever you call )

If /etc/redhat-release doesnot exit , echo system not running redhat or CentOS or redhat-release file is missing, then do nothing below

else

OS=`cat /etc/redhat-release | awk {'print $1}'`
if [ "$OS" != "CentOS" ]
then
echo "System runs on Redhat Linux.";
then use the up2date command
else if system runs Centos
then use the yum comand
done

Please try

cat /proc/version

and Grep for words "Red" and "CentOS" you can event get the version from the same.

Infact CentOS and RedHat systems show RedHat in /proc/version

root@server2 [~]# cat /etc/redhat-release
CentOS release 4.6 (Final)

root@server2 [~]# cat /proc/version
Linux version 2.6.9-42.0.3.ELsmp (buildsvn@build-i386) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)) #1 SMP Fri Oct 6 06:21:39 CDT 2006
root@server2 [~]#

Back to the original issue, in case we know that we are using either Redhat ( RHEL 4 or < ) Or CentOS , then the following script seems should do the trick.

=============================
OS=`cat /etc/redhat-release | awk {'print $1}'`
if [ "$OS" != "CentOS" ]
then
up2date -i dialog nmap ncurses ;
else
yum install -y dialog nmap ncurses
fi

=============================

Back to finding the linux flavour we use, lsb_release is the best command

root@server2 [~/downlaods]# lsb_release -a
LSB Version: :core-3.0-ia32:core-3.0-noarch:graphics-3.0-ia32:graphics-3.0-noarch
Distributor ID: CentOS
Description: CentOS release 4.6 (Final)
Release: 4.6
Codename: Final
root@server2 [~/downlaods]#

root@server2 [~/downlaods]# lsb_release -a | grep Distribu | awk {'print $3}'
CentOS
root@server2 [~/downlaods]#

Again note: lsb_release command does not work inside a VE ( say a openVz or Virtuozzo node )

i know is not strictly related to your quesiotn but it kinda hurts the eyes.
dont cat to awk, awk can read files

bad cat /etc/redhat-release | awk {'print $1}'
good awk {'print $1}' /etc/redhat-release

gcc's version is also a good way to identify the system you're running.

gcc -v

Thanks broli , you are right about awk.

----------------
redoubtable : Even if you have centOS , gcc -v , will show RedHat, same like /proc/version

[root@server1 htdocs]# gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)

[root@server1 htdocs]# cat /etc/redhat-release
CentOS release 4.6 (Final)
[root@server1 htdocs]#

So my final script looks like:

OS=`lsb_release -a | grep Distribu | awk {'print $3}'`
if [ "$OS" = "RedHatEnterpriseES" ]
then
up2date -i dialog nmap ncurses ;
else
echo " System is $OS "
echo " .............. skipping up2date .............."
fi

OS=`lsb_release -a | grep Distribu | awk {'print $3}'`
if [ "$OS" = "CentOS" ]
then
yum install -y dialog nmap ncurses ;
else
echo " System is $OS "
echo " .............. skipping yum .............."
fi

Any advise is welcome