Bash script for uniquely identifying linux and solaris platforms

This bash script creates a unique platform identifier string for linux (including cygwin) and solaris. It is useful for creating directory trees on network shared volumes that store data for different OS’s.
#!/bin/bash
#
# Determine the current linux or solaris platform and output a unique
# identifier string. Includes cygwin support.
#
# It is useful for creating directory trees on network shared volumes
# that store data for different OS's.
#
# Example:
#    % export PATH=/tools/`~/bin/platform.sh`/bin:"${PATH}"
#
# The format of the output is:
#   -<dist>-<ver>-<arch>
#   ^      ^      ^     ^
#   |      |      |     +----- architecture: x86_64, i86pc, etc.
#   |      |      +----------- version: 5.5, 4.7
#   |      +------------------ distribution: centos, rhel, nexentaos
#   +------------------------- platform: linux, sunos
#

# ================================================================
# Trim a string, remove internal spaces, convert to lower case.
# ================================================================
function get-platform-trim {
    local s=$(echo "$1" | tr -d '[ \t]' | tr 'A-Z' 'a-z')
    echo $s
}

# ================================================================
# Get the platform root name.
# ================================================================
function get-platform-root
{
    if which uname >/dev/null 2>&1 ; then
        # Greg Moeller reported that the original code didn't
        # work because the -o option is not available on solaris.
        # I modified the script to correctly identify that
        # case and recover by using the -s option.
        if uname -o >/dev/null 2>&1 ; then
            # Linux distro
            uname -o | tr 'A-Z' 'a-z'
        elif uname -s >/dev/null 2>&1 ; then
            # Solaris variant
            uname -s | tr 'A-Z' 'a-z'
        else
            echo "unkown"
        fi
    else
        echo "unkown"
    fi
}

# ================================================================
# Get the platform identifier.
# ================================================================
function get-platform
{
    plat=$(get-platform-root)
    case "$plat" in
        "gnu/linux")
            d=$(get-platform-trim "$(lsb_release -i)" | awk -F: '{print $2;}')
            r=$(get-platform-trim "$(lsb_release -r)" | awk -F: '{print $2;}')
            m=$(get-platform-trim "$(uname -m)")
            if [[ "$d" == "redhatenterprise"* ]] ; then
                # Need a little help for Red Hat because
                # they don't make the minor version obvious.
                d="rhel_${d:16}"  # keep the tail (e.g., es or client)
                x=$(get-platform-trim "$(lsb_release -c)" | \
                    awk -F: '{print $2;}' | \
                    sed -e 's/[^0-9]//g')
                r="$r.$x"
            fi
            echo "linux-$d-$r-$m"
            ;;
        "cygwin")
            x=$(get-platform-trim "$(uname)")
            echo "linux-$x"
            ;;
        "sunos")
            d=$(get-platform-trim "$(uname -v)")
            r=$(get-platform-trim "$(uname -r)")
            m=$(get-platform-trim "$(uname -m)")
            echo "sunos-$d-$r-$m"
            ;;
        "unknown")
            echo "unk-unk-unk-unk"
            ;;
        *)
            echo "$plat-unk-unk-unk"
            ;;
    esac
}

# ================================================================
# main
# ================================================================
get-platform

3 thoughts on “Bash script for uniquely identifying linux and solaris platforms”

  1. Your script doesn’t run on a Solaris system.

    uname doesn’t have a -o option in Solaris (apparently), so your function is blowing up.

    1. I am not surprised. It was never intended to work for a non-linux distribution. If you manage to get it working for solaris and want me to post it, I would be happy to.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.