Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I recognize which OS I am on in a bash script [duplicate]

Tags:

bash

I need to make script that behaves differently per system. Today it is possible to run bash even on microsoft windows, mac, linux, hp-ux, solaris etc...

How can I determine which of these operating systems I am on? I don't need exact version, I just need to know if I am on windows, linux, solaris...

like image 811
Petr Avatar asked Oct 05 '22 05:10

Petr


1 Answers

There is a standard shell command "uname" which returns the current platform as a string

To use this in a shell program a typical stanza might be

#!/bin/sh



if [ `uname` = "Linux" ] ;
then
    echo "we are on the operating system of Linux"
fi

if [ `uname` = "FreeBSD" ] ;
then
    echo "we are on the operating system of FreeBSD"
fi

More specific information is available but unfortunately it varies according to platform. On many versions of Linux ( and ISTR, Solaris ) there is a /etc/issue file which has the version name and number for the distribution installed. So on ubuntu

if [ -e "/etc/issue" ] ;
then
issue=`cat /etc/issue`
set -- $issue
if [ $1 = "Ubuntu" ] ;
then
    echo "we are on Ubuntu version " $2
fi
fi

This will give version information

like image 91
Vorsprung Avatar answered Oct 25 '22 20:10

Vorsprung