A script to detect system type

Hi forum,

So I am trying to determine the OS type with the following script:


#!/usr/bin/sh

OStype1=`uname -s`
Sunos1=SunOs

if [ "${OStype1}" = "Linux" ]
then
  echo "This system is Linux"
   exit 0
elif [ "${OStype1}" = "SunOs" ]
        then
                echo "This system is SunOs"
   exit 0
elif [ "${OStype1}" = "HP-UX" ]
        then
                echo "This system is HP-UX"
   exit 0

   else
   echo "This system is not supported"

 exit 0

fi


So far it is working ok for linux and hp-ux system types, but with no luck on Solaris.

Although the

OStype1=`uname -s

equals

SunOs

I still get this system is not supported


# sh -x test.sh
+ uname -s
OStype1=SunOS
Sunos1=SunOs
+ [ SunOS = Linux ]
+ [ SunOS = SunOs ]
+ [ SunOS = HP-UX ]
+ echo This system is not supported
This system is not supported
+ exit 0


Can you help me out by suggesting how to fix the script in order to work for Solaris?

---------- Post updated at 04:45 AM ---------- Previous update was at 04:29 AM ----------

Sorry to bother you, I have typed SunOs instead of SunOS. It is ok now.

Might I also suggest a case statement rather that if...then...elif...then...elif...then...elif...then...fi

Perhaps this might be neater:-

case "${OSType1}" in
   Linux)      echo "This is Linux"     ;;
   SunOS)      echo "This is Solaris"   ;;
   HP-UX)      echo "This is HPUX"      ;;
   AIX)        echo "This is the best." ;;
   *)          echo "This in unknown"   ;;
esac

You can add more statements between each choice. The last statement to be run for each choice ends in ;; and esac ends the whole case statement.

I hope that this helps,
Robin

1 Like