{"id":404,"date":"2012-02-29T01:13:56","date_gmt":"2012-02-29T01:13:56","guid":{"rendered":"http:\/\/joelinoff.com\/blog\/?p=404"},"modified":"2012-09-21T15:51:12","modified_gmt":"2012-09-21T15:51:12","slug":"bash-script-for-uniquely-identifying-linux-and-solaris-platforms","status":"publish","type":"post","link":"https:\/\/joelinoff.com\/blog\/?p=404","title":{"rendered":"Bash script for uniquely identifying linux and solaris platforms"},"content":{"rendered":"<p>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&#8217;s.<br \/>\n<!--more--><br \/>\n[crayon lang=&#8221;bash&#8221; toolbar=&#8221;always&#8221; title=&#8221;get-platform.sh&#8221;]<br \/>\n#!\/bin\/bash<br \/>\n#<br \/>\n# Determine the current linux or solaris platform and output a unique<br \/>\n# identifier string. Includes cygwin support.<br \/>\n#<br \/>\n# It is useful for creating directory trees on network shared volumes<br \/>\n# that store data for different OS&#8217;s.<br \/>\n#<br \/>\n# Example:<br \/>\n#    % export PATH=\/tools\/`~\/bin\/platform.sh`\/bin:&#8221;${PATH}&#8221;<br \/>\n#<br \/>\n# The format of the output is:<br \/>\n#   <plat>&#8211;<dist>&#8211;<ver>&#8211;<arch><br \/>\n#   ^      ^      ^     ^<br \/>\n#   |      |      |     +&#8212;&#8211; architecture: x86_64, i86pc, etc.<br \/>\n#   |      |      +&#8212;&#8212;&#8212;&#8211; version: 5.5, 4.7<br \/>\n#   |      +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; distribution: centos, rhel, nexentaos<br \/>\n#   +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- platform: linux, sunos<br \/>\n#<\/p>\n<p># ================================================================<br \/>\n# Trim a string, remove internal spaces, convert to lower case.<br \/>\n# ================================================================<br \/>\nfunction get-platform-trim {<br \/>\n    local s=$(echo &#8220;$1&#8221; | tr -d &#8216;[ \\t]&#8217; | tr &#8216;A-Z&#8217; &#8216;a-z&#8217;)<br \/>\n    echo $s<br \/>\n}<\/p>\n<p># ================================================================<br \/>\n# Get the platform root name.<br \/>\n# ================================================================<br \/>\nfunction get-platform-root<br \/>\n{<br \/>\n    if which uname >\/dev\/null 2>&#038;1 ; then<br \/>\n        # Greg Moeller reported that the original code didn&#8217;t<br \/>\n        # work because the -o option is not available on solaris.<br \/>\n        # I modified the script to correctly identify that<br \/>\n        # case and recover by using the -s option.<br \/>\n        if uname -o >\/dev\/null 2>&#038;1 ; then<br \/>\n            # Linux distro<br \/>\n            uname -o | tr &#8216;A-Z&#8217; &#8216;a-z&#8217;<br \/>\n        elif uname -s >\/dev\/null 2>&#038;1 ; then<br \/>\n            # Solaris variant<br \/>\n            uname -s | tr &#8216;A-Z&#8217; &#8216;a-z&#8217;<br \/>\n        else<br \/>\n            echo &#8220;unkown&#8221;<br \/>\n        fi<br \/>\n    else<br \/>\n        echo &#8220;unkown&#8221;<br \/>\n    fi<br \/>\n}<\/p>\n<p># ================================================================<br \/>\n# Get the platform identifier.<br \/>\n# ================================================================<br \/>\nfunction get-platform<br \/>\n{<br \/>\n    plat=$(get-platform-root)<br \/>\n    case &#8220;$plat&#8221; in<br \/>\n        &#8220;gnu\/linux&#8221;)<br \/>\n            d=$(get-platform-trim &#8220;$(lsb_release -i)&#8221; | awk -F: &#8216;{print $2;}&#8217;)<br \/>\n            r=$(get-platform-trim &#8220;$(lsb_release -r)&#8221; | awk -F: &#8216;{print $2;}&#8217;)<br \/>\n            m=$(get-platform-trim &#8220;$(uname -m)&#8221;)<br \/>\n            if [[ &#8220;$d&#8221; == &#8220;redhatenterprise&#8221;* ]] ; then<br \/>\n                # Need a little help for Red Hat because<br \/>\n                # they don&#8217;t make the minor version obvious.<br \/>\n                d=&#8221;rhel_${d:16}&#8221;  # keep the tail (e.g., es or client)<br \/>\n                x=$(get-platform-trim &#8220;$(lsb_release -c)&#8221; | \\<br \/>\n                    awk -F: &#8216;{print $2;}&#8217; | \\<br \/>\n                    sed -e &#8216;s\/[^0-9]\/\/g&#8217;)<br \/>\n                r=&#8221;$r.$x&#8221;<br \/>\n            fi<br \/>\n            echo &#8220;linux-$d-$r-$m&#8221;<br \/>\n            ;;<br \/>\n        &#8220;cygwin&#8221;)<br \/>\n            x=$(get-platform-trim &#8220;$(uname)&#8221;)<br \/>\n            echo &#8220;linux-$x&#8221;<br \/>\n            ;;<br \/>\n        &#8220;sunos&#8221;)<br \/>\n            d=$(get-platform-trim &#8220;$(uname -v)&#8221;)<br \/>\n            r=$(get-platform-trim &#8220;$(uname -r)&#8221;)<br \/>\n            m=$(get-platform-trim &#8220;$(uname -m)&#8221;)<br \/>\n            echo &#8220;sunos-$d-$r-$m&#8221;<br \/>\n            ;;<br \/>\n        &#8220;unknown&#8221;)<br \/>\n            echo &#8220;unk-unk-unk-unk&#8221;<br \/>\n            ;;<br \/>\n        *)<br \/>\n            echo &#8220;$plat-unk-unk-unk&#8221;<br \/>\n            ;;<br \/>\n    esac<br \/>\n}<\/p>\n<p># ================================================================<br \/>\n# main<br \/>\n# ================================================================<br \/>\nget-platform<br \/>\n[\/crayon]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/404"}],"collection":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":10,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions\/810"}],"wp:attachment":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}