File size: 5,129 Bytes
fe5d4e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
#
# vim:et:ft=sh:sts=2:sw=2
#
#/**
# * Licensed to the Apache Software Foundation (ASF) under one
# * or more contributor license agreements.  See the NOTICE file
# * distributed with this work for additional information
# * regarding copyright ownership.  The ASF licenses this file
# * to you under the Apache License, Version 2.0 (the
# * "License"); you may not use this file except in compliance
# * with the License.  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */

ARGV0=`basename "$0"`
LSB_RELEASE="/etc/lsb-release"
VERSIONS_SHELLS="/bin/bash /bin/sh"

true; TRUE=$?
false; FALSE=$?
ERROR=2

UNAME_R=`uname -r`
UNAME_S=`uname -s`

__versions_haveStrings=${ERROR}

versions_osName() {
  os_name_='unrecognized'
  os_system_=${UNAME_S}
  os_release_=${UNAME_R}
  case ${os_system_} in
    CYGWIN_NT-*) os_name_='Cygwin' ;;
    Darwin)
      os_name_=`/usr/bin/sw_vers -productName`
      os_version_=`versions_osVersion`
      case ${os_version_} in
        10.4|10.4.[0-9]*) os_name_='Mac OS X Tiger' ;;
        10.5|10.5.[0-9]*) os_name_='Mac OS X Leopard' ;;
        10.6|10.6.[0-9]*) os_name_='Mac OS X Snow Leopard' ;;
        10.7|10.7.[0-9]*) os_name_='Mac OS X Lion' ;;
        10.8|10.8.[0-9]*) os_name_='Mac OS X Mountain Lion' ;;
        10.9|10.9.[0-9]*) os_name_='Mac OS X Mavericks' ;;
        10.10|10.10.[0-9]*) os_name_='Mac OS X Yosemite' ;;
        10.11|10.11.[0-9]*) os_name_='Mac OS X El Capitan' ;;
        10.12|10.12.[0-9]*) os_name_='macOS Sierra' ;;
        10.13|10.13.[0-9]*) os_name_='macOS High Sierra' ;;
        *) os_name_='macOS' ;;
      esac
      ;;
    FreeBSD) os_name_='FreeBSD' ;;
    Linux) os_name_='Linux' ;;
    SunOS)
      os_name_='SunOS'
      if [ -r '/etc/release' ]; then
        if grep 'OpenSolaris' /etc/release >/dev/null; then
          os_name_='OpenSolaris'
        else
          os_name_='Solaris'
        fi
      fi
      ;;
  esac

  echo ${os_name_}
  unset os_name_ os_system_ os_release_ os_version_
}

versions_osVersion() {
  os_version_='unrecognized'
  os_system_=${UNAME_S}
  os_release_=${UNAME_R}
  case ${os_system_} in
    CYGWIN_NT-*)
      os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]\.[0-9]*\).*'`
      ;;
    Darwin)
      os_version_=`/usr/bin/sw_vers -productVersion`
      ;;
    FreeBSD)
      os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]*\)-.*'`
      ;;
    Linux)
      if [ -r '/etc/os-release' ]; then
          os_version_=`awk -F= '$1~/PRETTY_NAME/{print $2}' /etc/os-release \
            |sed 's/"//g'`
      elif [ -r '/etc/redhat-release' ]; then
        os_version_=`cat /etc/redhat-release`
      elif [ -r '/etc/SuSE-release' ]; then
        os_version_=`head -n 1 /etc/SuSE-release`
      elif [ -r "${LSB_RELEASE}" ]; then
        if grep -q 'DISTRIB_ID=Ubuntu' "${LSB_RELEASE}"; then
          # shellcheck disable=SC2002
          os_version_=`cat "${LSB_RELEASE}" \
            |awk -F= '$1~/DISTRIB_DESCRIPTION/{print $2}' \
            |sed 's/"//g;s/ /-/g'`
        fi
      fi
      ;;
    SunOS)
      if [ -r '/etc/release' ]; then
        if grep 'OpenSolaris' /etc/release >/dev/null; then  # OpenSolaris
          os_version_=`grep 'OpenSolaris' /etc/release |awk '{print $2"("$3")"}'`
        else  # Solaris
          major_=`echo "${os_release_}" |sed 's/[0-9]*\.\([0-9]*\)/\1/'`
          minor_=`grep Solaris /etc/release |sed 's/[^u]*\(u[0-9]*\).*/\1/'`
          os_version_="${major_}${minor_}"
        fi
      fi
      ;;
  esac

  echo "${os_version_}"
  unset os_release_ os_system_ os_version_ major_ minor_
}

versions_shellVersion() {
  shell_=$1

  shell_present_=${FALSE}
  case "${shell_}" in
    ash) [ -x '/bin/busybox' ] && shell_present_=${TRUE} ;;
    *) [ -x "${shell_}" ] && shell_present_=${TRUE} ;;
  esac
  if [ ${shell_present_} -eq ${FALSE} ]; then
    echo 'not installed'
    return ${FALSE}
  fi

  version_=''
  case ${shell_} in
    */sh)
      # This could be one of any number of shells. Try until one fits.
      version_=''
      [ -z "${version_}" ] && version_=`versions_shell_bash "${shell_}"`
      ;;
    */bash) version_=`versions_shell_bash "${shell_}"` ;;
    *) version_='invalid'
  esac

  echo "${version_:-unknown}"
  unset shell_ version_
}

versions_shell_bash() {
  $1 --version : 2>&1 |grep 'GNU bash' |sed 's/.*version \([^ ]*\).*/\1/'
}

versions_main() {
  # Treat unset variables as an error.
  set -u

  os_name=`versions_osName`
  os_version=`versions_osVersion`
  echo "os: ${os_name} version: ${os_version}"

  for shell in ${VERSIONS_SHELLS}; do
    shell_version=`versions_shellVersion "${shell}"`
    echo "shell: ${shell} version: ${shell_version}"
  done
}

if [ "${ARGV0}" = 'versions' ]; then
  versions_main "$@"
fi