Detect user’s OS using bash

Here is an approach to detect the user’s operating system using a bash script. This might be useful during installation routines of your software. Inspired by StackOverflow answers.

 

# determine operating system, alternative: use $OSTYPE
printf "Detecting OS\n"
platform='unknown'
OS=`uname`
case $OS in
    'Linux')
        platform='Linux'
        # do something for linux...
        ;;
    'FreeBSD')
        platform='FreeBSD'
        ;;
    'WindowsNT')
        platform='Windows'
        ;;
    'Darwin') 
        platform='Mac'
        ;;
    'SunOS')
        platform='Solaris'
        ;;
    *)
        printf "The operating systen could not be determined.\nPlease select\n \
            1 - Apple OsX\n\
            2 - Microsoft Windows\n\
            3 - Linux\n"
        read os_num
        case $os_num in
            '1')
                platform='Mac'
                ;;
            '2')
                platform='Windows'
                ;;
            '3')
                platform='Linux'
                ;;
            *)
                printf "Unknown OS, exiting.\n\n"
                exit
                ;;
        esac
        ;;
esac
printf "Found OS ${platform}\n\n"