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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#!/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: # <plat>-<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 |
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.
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.
Thank you for pointing this out.
I think that I fixed the problem. I needed a Solaris variant to test it and found OpenIndiana but it took awhile.