How can I find out in which OS I am included in a bash script

I need to make a script that behaves differently for each system. Today you can run bash even in windows microsoft, mac, linux, hp-ux, solaris, etc.

How can I determine which of these operating systems I am in? I don’t need the exact version, I just need to know if I am on windows, Linux, Solaris ...

+4
source share
3 answers

There is a standard "uname" command shell that 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 depends on the platform. In many versions of Linux (and ISTR, Solaris) there is a file / etc / issue, which has a name and version number for the installed distribution. So 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

+2
source

I would look at the conclusion

 uname -a 

and find specific lines to help you identify the system.

Or more specific

 uname -s 

On Windows, do you mean something like cygwin?

0
source

bash has a global var called $OSTYPE . Enter echo $OSTYPE to see:

 echo "$OSTYPE" // linux-gnu 

On the bash man page:

OSTYPE Automatically sets a line describing the operating system on which bash is running. The default value is system dependent.


An alternative is to use the uname command (without any arguments) or uname -s so that it matches the default uname to -s .

Linux example

 uname // Linux 
0
source

Source: https://habr.com/ru/post/1478953/


All Articles