blob: ad24362114cc5985e81408ca0828d6f8a9ca6494 [file] [log] [blame]
kate.ward2f3cad92008-10-21 23:29:23 +00001#! /bin/sh
2# $Id$
3# vim:et:ft=sh:sts=2:sw=2
4#
5# Copyright 2008 Kate Ward. All Rights Reserved.
6# Released under the LGPL (GNU Lesser General Public License)
7#
8# Author: kate.ward@forestent.com (Kate Ward)
9#
10# This library provides reusable functions that determine actual names and
11# versions of installed shells and the OS. The library can also be run as a
12# script if set execuatable.
13
14VERSIONS_SHELLS='/bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/zsh'
15
16#------------------------------------------------------------------------------
17# functions
18#
19
20versions_osName()
21{
22 os_name_='unrecognized'
23 os_system_=`uname -s`
24 case ${os_system_} in
25 Cygwin) os_name_='Cygwin' ;;
26 Darwin) os_name_='Mac OS X' ;;
27 Linux) os_name_='Linux' ;;
28 SunOS) os_name_='Solaris' ;;
29 esac
30 echo ${os_name_}
31 unset os_name_ os_system_
32}
33
34versions_osVersion()
35{
36 os_version_='unrecognized'
37 os_system_=`uname -s`
38 os_release_=`uname -r`
39 case ${os_system_} in
40 Cygwin) os_version_='unknown' ;;
41 Darwin)
42 major_='10'
43 sub_=`echo ${os_release_} |\
44 sed 's/^[0-9]*\.\([0-9]*\)\.[0-9]*$/\1/'`
45 case ${os_release_} in
46 8.*) minor_='4' ;;
47 9.*) minor_='5' ;;
48 *) minor_='X'; sub_='X' ;;
49 esac
50 os_version_="${major_}.${minor_}.${sub_}"
51 ;;
52 Linux)
53 if [ -r '/etc/lsb-release' ]; then
54 os_version=`. /etc/lsb-release \
55 && echo "${DISTRIB_ID}-${DISTRIB_RELEASE}"`
56 fi
57 ;;
58 SunOS) os_version_=`echo ${os_release_} |sed 's/[0-9]*\.\([0-9]*\)/\1/'` ;;
59 esac
60 echo ${os_version_}
61 unset os_name_ os_release_ os_version_ major_ minor_ sub_
62}
63
64versions_shellVersion()
65{
66 shell_=$1
67
68 if [ ! -x "${shell_}" ]; then
69 echo 'not installed'
70 return
71 fi
72
73 version_='unknown'
74 case ${shell_} in
75 */sh) # this could be one of any number of shells. try until one fits.
76 version_=`versions_shell_bash ${shell_}`
77 # dash cannot be self determined yet
78 [ -z "${version_}" ] && version_=`versions_shell_ksh ${shell_}`
79 # pdksh is covered in versions_shell_ksh()
80 [ -z "${version_}" ] && version_=`versions_shell_zsh ${shell_}`
81 ;;
82 */bash) version_=`versions_shell_bash ${shell_}` ;;
83 */dash)
84 # simply assuming Ubuntu Linux until somebody comes up with a better
85 # test. the following test will return an empty string if dash is not
86 # installed.
87 version_=`versions_shell_dash`
88 ;;
89 */ksh) version_=`versions_shell_ksh ${shell_}` ;;
90 */pdksh) version_=`versions_shell_pdksh ${shell_}` ;;
91 */zsh) version_=`versions_shell_zsh ${shell_}` ;;
92 *) version_='invalid'
93 esac
94 echo ${version_}
95 unset shell_ version_
96}
97
98versions_shell_bash()
99{
100 $1 --version 2>&1 |grep 'GNU bash' |sed 's/.*version \([^ ]*\).*/\1/'
101}
102
103versions_shell_dash()
104{
105 dpkg -l |grep ' dash ' |awk '{print $3}'
106}
107
108versions_shell_ksh()
109{
110 versions_shell_=$1
111
112 versions_version_=`strings ${versions_shell_} 2>&1 \
113 |grep Version \
114 |sed 's/^.*Version \(.*\)$/\1/;s/ s+ \$$//;s/ /-/g'`
115 [ -z "${versions_version_}" ] \
116 && versions_version_=`versions_shell_pdksh ${versions_shell_}`
117 echo ${versions_version_}
118
119 unset versions_shell_ versions_version_
120}
121
122versions_shell_pdksh()
123{
124 strings $1 2>&1 \
125 |grep 'PD KSH' \
126 |sed -e 's/.*PD KSH \(.*\)/\1/;s/ /-/g'
127}
128
129versions_shell_zsh()
130{
131 echo 'echo ${ZSH_VERSION}' |$1
132}
133
134#------------------------------------------------------------------------------
135# main
136#
137
138versions_main()
139{
140 # treat unset variables as an error
141 set -u
142
143 os_name=`versions_osName`
144 os_version=`versions_osVersion`
145 echo "os: ${os_name} version: ${os_version}"
146
147 # note: /bin/sh not included as it is nearly always a sym-link, and if it
148 # isn't it is too much trouble to figure out what it is.
149 for shell in ${VERSIONS_SHELLS}; do
150 shell_version=`versions_shellVersion ${shell}`
151 echo "shell: ${shell} version: ${shell_version}"
152 done
153}
154
155if [ "`basename $0`" = 'versions' ]; then
156 versions_main "$@"
157fi