1#! /bin/sh 2# vim:et:ft=sh:sts=2:sw=2 3# 4# Versions determines the versions of all installed shells. 5# 6# Copyright 2008-2020 Kate Ward. All Rights Reserved. 7# Released under the Apache 2.0 License. 8# 9# Author: kate.ward@forestent.com (Kate Ward) 10# https://github.com/kward/shlib 11# 12# This library provides reusable functions that determine actual names and 13# versions of installed shells and the OS. The library can also be run as a 14# script if set executable. 15# 16# Disable checks that aren't fully portable (POSIX != portable). 17# shellcheck disable=SC2006 18 19ARGV0=`basename "$0"` 20LSB_RELEASE='/etc/lsb-release' 21VERSIONS_SHELLS='ash /bin/bash /bin/dash /bin/ksh /bin/mksh /bin/pdksh /bin/zsh /usr/xpg4/bin/sh /bin/sh /sbin/sh' 22 23true; TRUE=$? 24false; FALSE=$? 25ERROR=2 26 27UNAME_R=`uname -r` 28UNAME_S=`uname -s` 29 30__versions_haveStrings=${ERROR} 31 32versions_osName() { 33 os_name_='unrecognized' 34 os_system_=${UNAME_S} 35 os_release_=${UNAME_R} 36 case ${os_system_} in 37 CYGWIN_NT-*) os_name_='Cygwin' ;; 38 Darwin) 39 os_name_=`/usr/bin/sw_vers -productName` 40 os_version_=`versions_osVersion` 41 case ${os_version_} in 42 10.4|10.4.[0-9]*) os_name_='Mac OS X Tiger' ;; 43 10.5|10.5.[0-9]*) os_name_='Mac OS X Leopard' ;; 44 10.6|10.6.[0-9]*) os_name_='Mac OS X Snow Leopard' ;; 45 10.7|10.7.[0-9]*) os_name_='Mac OS X Lion' ;; 46 10.8|10.8.[0-9]*) os_name_='Mac OS X Mountain Lion' ;; 47 10.9|10.9.[0-9]*) os_name_='Mac OS X Mavericks' ;; 48 10.10|10.10.[0-9]*) os_name_='Mac OS X Yosemite' ;; 49 10.11|10.11.[0-9]*) os_name_='Mac OS X El Capitan' ;; 50 10.12|10.12.[0-9]*) os_name_='macOS Sierra' ;; 51 10.13|10.13.[0-9]*) os_name_='macOS High Sierra' ;; 52 10.14|10.14.[0-9]*) os_name_='macOS Mojave' ;; 53 10.15|10.15.[0-9]*) os_name_='macOS Catalina' ;; 54 *) os_name_='macOS' ;; 55 esac 56 ;; 57 FreeBSD) os_name_='FreeBSD' ;; 58 Linux) os_name_='Linux' ;; 59 SunOS) 60 os_name_='SunOS' 61 if [ -r '/etc/release' ]; then 62 if grep 'OpenSolaris' /etc/release >/dev/null; then 63 os_name_='OpenSolaris' 64 else 65 os_name_='Solaris' 66 fi 67 fi 68 ;; 69 esac 70 71 echo ${os_name_} 72 unset os_name_ os_system_ os_release_ os_version_ 73} 74 75versions_osVersion() { 76 os_version_='unrecognized' 77 os_system_=${UNAME_S} 78 os_release_=${UNAME_R} 79 case ${os_system_} in 80 CYGWIN_NT-*) 81 os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]\.[0-9]*\).*'` 82 ;; 83 Darwin) 84 os_version_=`/usr/bin/sw_vers -productVersion` 85 ;; 86 FreeBSD) 87 os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]*\)-.*'` 88 ;; 89 Linux) 90 if [ -r '/etc/os-release' ]; then 91 os_version_=`awk -F= '$1~/PRETTY_NAME/{print $2}' /etc/os-release \ 92 |sed 's/"//g'` 93 elif [ -r '/etc/redhat-release' ]; then 94 os_version_=`cat /etc/redhat-release` 95 elif [ -r '/etc/SuSE-release' ]; then 96 os_version_=`head -n 1 /etc/SuSE-release` 97 elif [ -r "${LSB_RELEASE}" ]; then 98 if grep -q 'DISTRIB_ID=Ubuntu' "${LSB_RELEASE}"; then 99 # shellcheck disable=SC2002 100 os_version_=`cat "${LSB_RELEASE}" \ 101 |awk -F= '$1~/DISTRIB_DESCRIPTION/{print $2}' \ 102 |sed 's/"//g;s/ /-/g'` 103 fi 104 fi 105 ;; 106 SunOS) 107 if [ -r '/etc/release' ]; then 108 if grep 'OpenSolaris' /etc/release >/dev/null; then # OpenSolaris 109 os_version_=`grep 'OpenSolaris' /etc/release |awk '{print $2"("$3")"}'` 110 else # Solaris 111 major_=`echo "${os_release_}" |sed 's/[0-9]*\.\([0-9]*\)/\1/'` 112 minor_=`grep Solaris /etc/release |sed 's/[^u]*\(u[0-9]*\).*/\1/'` 113 os_version_="${major_}${minor_}" 114 fi 115 fi 116 ;; 117 esac 118 119 echo "${os_version_}" 120 unset os_release_ os_system_ os_version_ major_ minor_ 121} 122 123versions_shellVersion() { 124 shell_=$1 125 126 shell_present_=${FALSE} 127 case "${shell_}" in 128 ash) [ -x '/bin/busybox' ] && shell_present_=${TRUE} ;; 129 *) [ -x "${shell_}" ] && shell_present_=${TRUE} ;; 130 esac 131 if [ ${shell_present_} -eq ${FALSE} ]; then 132 echo 'not installed' 133 return ${FALSE} 134 fi 135 136 version_='' 137 case ${shell_} in 138 # SunOS shells. 139 /sbin/sh) ;; 140 /usr/xpg4/bin/sh) version_=`versions_shell_xpg4 "${shell_}"` ;; 141 142 # Generic shell. 143 */sh) 144 # This could be one of any number of shells. Try until one fits. 145 version_='' 146 [ -z "${version_}" ] && version_=`versions_shell_bash "${shell_}"` 147 # dash cannot be self determined yet 148 [ -z "${version_}" ] && version_=`versions_shell_ksh "${shell_}"` 149 # pdksh is covered in versions_shell_ksh() 150 [ -z "${version_}" ] && version_=`versions_shell_xpg4 "${shell_}"` 151 [ -z "${version_}" ] && version_=`versions_shell_zsh "${shell_}"` 152 ;; 153 154 # Specific shells. 155 ash) version_=`versions_shell_ash "${shell_}"` ;; 156 # bash - Bourne Again SHell (https://www.gnu.org/software/bash/) 157 */bash) version_=`versions_shell_bash "${shell_}"` ;; 158 */dash) version_=`versions_shell_dash` ;; 159 # ksh - KornShell (http://www.kornshell.com/) 160 */ksh) version_=`versions_shell_ksh "${shell_}"` ;; 161 # mksh - MirBSD Korn Shell (http://www.mirbsd.org/mksh.htm) 162 */mksh) version_=`versions_shell_ksh "${shell_}"` ;; 163 # pdksh - Public Domain Korn Shell (http://web.cs.mun.ca/~michael/pdksh/) 164 */pdksh) version_=`versions_shell_pdksh "${shell_}"` ;; 165 # zsh (https://www.zsh.org/) 166 */zsh) version_=`versions_shell_zsh "${shell_}"` ;; 167 168 # Unrecognized shell. 169 *) version_='invalid' 170 esac 171 172 echo "${version_:-unknown}" 173 unset shell_ version_ 174} 175 176# The ash shell is included in BusyBox. 177versions_shell_ash() { 178 busybox --help |head -1 |sed 's/BusyBox v\([0-9.]*\) .*/\1/' 179} 180 181versions_shell_bash() { 182 $1 --version : 2>&1 |grep 'GNU bash' |sed 's/.*version \([^ ]*\).*/\1/' 183} 184 185# Assuming Ubuntu Linux until somebody comes up with a better test. The 186# following test will return an empty string if dash is not installed. 187versions_shell_dash() { 188 eval dpkg >/dev/null 2>&1 189 [ $? -eq 127 ] && return # Return if dpkg not found. 190 191 dpkg -l |grep ' dash ' |awk '{print $3}' 192} 193 194versions_shell_ksh() { 195 versions_shell_=$1 196 versions_version_='' 197 198 # Try a few different ways to figure out the version. 199 versions_version_=`${versions_shell_} --version : 2>&1` 200 # shellcheck disable=SC2181 201 if [ $? -eq 0 ]; then 202 versions_version_=`echo "${versions_version_}" \ 203 |sed 's/.*\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/'` 204 else 205 versions_version_='' 206 fi 207 if [ -z "${versions_version_}" ]; then 208 # shellcheck disable=SC2016 209 versions_version_=`${versions_shell_} -c 'echo ${KSH_VERSION}'` 210 fi 211 if [ -z "${versions_version_}" ]; then 212 _versions_have_strings 213 versions_version_=`strings "${versions_shell_}" 2>&1 \ 214 |grep Version \ 215 |sed 's/^.*Version \(.*\)$/\1/;s/ s+ \$$//;s/ /-/g'` 216 fi 217 if [ -z "${versions_version_}" ]; then 218 versions_version_=`versions_shell_pdksh "${versions_shell_}"` 219 fi 220 221 echo "${versions_version_}" 222 unset versions_shell_ versions_version_ 223} 224 225# mksh - MirBSD Korn Shell (http://www.mirbsd.org/mksh.htm) 226# mksh is a successor to pdksh (Public Domain Korn Shell). 227versions_shell_mksh() { 228 versions_shell_ksh 229} 230 231# pdksh - Public Domain Korn Shell 232# pdksh is an obsolete shell, which was replaced by mksh (among others). 233versions_shell_pdksh() { 234 _versions_have_strings 235 strings "$1" 2>&1 \ 236 |grep 'PD KSH' \ 237 |sed -e 's/.*PD KSH \(.*\)/\1/;s/ /-/g' 238} 239 240versions_shell_xpg4() { 241 _versions_have_strings 242 strings "$1" 2>&1 \ 243 |grep 'Version' \ 244 |sed -e 's/^@(#)Version //' 245} 246 247versions_shell_zsh() { 248 versions_shell_=$1 249 250 # Try a few different ways to figure out the version. 251 # shellcheck disable=SC2016 252 versions_version_=`echo 'echo ${ZSH_VERSION}' |${versions_shell_}` 253 if [ -z "${versions_version_}" ]; then 254 versions_version_=`${versions_shell_} --version : 2>&1` 255 # shellcheck disable=SC2181 256 if [ $? -eq 0 ]; then 257 versions_version_=`echo "${versions_version_}" |awk '{print $2}'` 258 else 259 versions_version_='' 260 fi 261 fi 262 263 echo "${versions_version_}" 264 unset versions_shell_ versions_version_ 265} 266 267# Determine if the 'strings' binary installed. 268_versions_have_strings() { 269 [ ${__versions_haveStrings} -ne ${ERROR} ] && return 270 if eval strings /dev/null >/dev/null 2>&1; then 271 __versions_haveStrings=${TRUE} 272 return 273 fi 274 275 echo 'WARN: strings not installed. try installing binutils?' >&2 276 __versions_haveStrings=${FALSE} 277} 278 279versions_main() { 280 # Treat unset variables as an error. 281 set -u 282 283 os_name=`versions_osName` 284 os_version=`versions_osVersion` 285 echo "os: ${os_name} version: ${os_version}" 286 287 for shell in ${VERSIONS_SHELLS}; do 288 shell_version=`versions_shellVersion "${shell}"` 289 echo "shell: ${shell} version: ${shell_version}" 290 done 291} 292 293if [ "${ARGV0}" = 'versions' ]; then 294 versions_main "$@" 295fi 296