aboutsummaryrefslogtreecommitdiff
path: root/source/1.0/lib/versions
blob: 24757f156cefe6abb6179c3b80e6e2b67be7a089 (plain)
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
#! /bin/sh
# $Id$
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
#
# Author: kate.ward@forestent.com (Kate Ward)
#
# This library provides reusable functions that determine actual names and
# versions of installed shells and the OS. The library can also be run as a
# script if set execuatable.

VERSIONS_SHELLS='/bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/sh /bin/zsh'

#------------------------------------------------------------------------------
# functions
#

versions_osName()
{
  os_name_='unrecognized'
  os_system_=`uname -s`
  case ${os_system_} in
    Cygwin) os_name_='Cygwin' ;;
    Darwin) os_name_='Mac OS X' ;;
    Linux) os_name_='Linux' ;;
    SunOS) os_name_='Solaris' ;;
  esac
  echo ${os_name_}
  unset os_name_ os_system_
}

versions_osVersion()
{
  os_version_='unrecognized'
  os_system_=`uname -s`
  os_release_=`uname -r`
  case ${os_system_} in
    Cygwin) os_version_='unknown' ;;
    Darwin)
      major_='10'
      sub_=`echo ${os_release_} |\
          sed 's/^[0-9]*\.\([0-9]*\)\.[0-9]*$/\1/'`
      case ${os_release_} in
        8.*) minor_='4' ;;
        9.*) minor_='5' ;;
        *) minor_='X'; sub_='X' ;;
      esac
      os_version_="${major_}.${minor_}.${sub_}"
      ;;
    Linux)
      if [ -r '/etc/lsb-release' ]; then
        os_version_=`. /etc/lsb-release && \
            echo "${DISTRIB_ID:-}-${DISTRIB_RELEASE:-}"`
      fi
      if [ "${os_version_}" = '-' ]; then
        os_version_=''
        if [ -r '/etc/redhat-release' ]; then
          os_version_=`cat /etc/redhat-release`
        fi
      fi
      ;;
    SunOS) os_version_=`echo ${os_release_} |sed 's/[0-9]*\.\([0-9]*\)/\1/'` ;;
  esac
  echo ${os_version_}
  unset os_name_ os_release_ os_version_ major_ minor_ sub_
}

versions_shellVersion()
{
  shell_=$1

  if [ ! -x "${shell_}" ]; then
    echo 'not installed'
    return
  fi

  version_=''
  case ${shell_} in
    */sh)
      # TODO(kward): fix this
      ## this could be one of any number of shells. try until one fits.
      #version_=`versions_shell_bash ${shell_}`
      ## dash cannot be self determined yet
      #[ -z "${version_}" ] && version_=`versions_shell_ksh ${shell_}`
      ## pdksh is covered in versions_shell_ksh()
      #[ -z "${version_}" ] && version_=`versions_shell_zsh ${shell_}`
      ;;
    */bash) version_=`versions_shell_bash ${shell_}` ;;
    */dash)
      # simply assuming Ubuntu Linux until somebody comes up with a better
      # test. the following test will return an empty string if dash is not
      # installed.
      version_=`versions_shell_dash`
      ;;
    */ksh) version_=`versions_shell_ksh ${shell_}` ;;
    */pdksh) version_=`versions_shell_pdksh ${shell_}` ;;
    */zsh) version_=`versions_shell_zsh ${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_shell_dash()
{
  dpkg -l |grep ' dash ' |awk '{print $3}'
}

versions_shell_ksh()
{
  versions_shell_=$1

  versions_version_=`strings ${versions_shell_} 2>&1 \
      |grep Version \
      |sed 's/^.*Version \(.*\)$/\1/;s/ s+ \$$//;s/ /-/g'`
  [ -z "${versions_version_}" ] \
      && versions_version_=`versions_shell_pdksh ${versions_shell_}`
  echo ${versions_version_}

  unset versions_shell_ versions_version_
}

versions_shell_pdksh()
{
  strings $1 2>&1 \
  |grep 'PD KSH' \
  |sed -e 's/.*PD KSH \(.*\)/\1/;s/ /-/g'
}

versions_shell_zsh()
{
  echo 'echo ${ZSH_VERSION}' |$1
}

#------------------------------------------------------------------------------
# main
#

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 [ "`basename $0`" = 'versions' ]; then
  versions_main "$@"
fi