Sat Aug 19 18:36:31 2017 UTC ()
Parse the arguments to sys_info(1) a bit differently, using getopts(1)

Thanks to Paul Goyette for the nudge


(agc)
diff -r1.2 -r1.3 src/usr.bin/sys_info/sys_info.sh

cvs diff -r1.2 -r1.3 src/usr.bin/sys_info/sys_info.sh (switch to unified diff)

--- src/usr.bin/sys_info/sys_info.sh 2017/08/19 03:06:50 1.2
+++ src/usr.bin/sys_info/sys_info.sh 2017/08/19 18:36:31 1.3
@@ -1,150 +1,151 @@ @@ -1,150 +1,151 @@
1#! /bin/sh 1#! /bin/sh
2 2
3# $NetBSD: sys_info.sh,v 1.2 2017/08/19 03:06:50 agc Exp $ 3# $NetBSD: sys_info.sh,v 1.3 2017/08/19 18:36:31 agc Exp $
4 4
5# Copyright (c) 2016 Alistair Crooks <agc@NetBSD.org> 5# Copyright (c) 2016 Alistair Crooks <agc@NetBSD.org>
6# All rights reserved. 6# All rights reserved.
7# 7#
8# Redistribution and use in source and binary forms, with or without 8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions 9# modification, are permitted provided that the following conditions
10# are met: 10# are met:
11# 1. Redistributions of source code must retain the above copyright 11# 1. Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer. 12# notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright 13# 2. Redistributions in binary form must reproduce the above copyright
14# notice, this list of conditions and the following disclaimer in the 14# notice, this list of conditions and the following disclaimer in the
15# documentation and/or other materials provided with the distribution. 15# documentation and/or other materials provided with the distribution.
16# 16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27# 27#
28 28
29LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-/usr/lib:/usr/X11R7/lib} 29LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-/usr/lib:/usr/X11R7/lib}
30 30
31# print out the version for the given argument 31# print out the version for the given argument
32getversion() { 32getversion() {
33 case "$1" in 33 case "$1" in
34 awk) 34 awk)
35 awk --version | awk '{ print $1 "-" $3 }' 35 awk --version | awk '{ print $1 "-" $3 }'
36 ;; 36 ;;
37 bind|named) 37 bind|named)
38 named -v | awk '{ gsub("-", "", $2); gsub("P", "pl", $2); print tolower($1) "-" $2 }' 38 named -v | awk '{ gsub("-", "", $2); gsub("P", "pl", $2); print tolower($1) "-" $2 }'
39 ;; 39 ;;
40 bzip2) 40 bzip2)
41 bzip2 --help 2>&1 | awk '{ sub(",", "", $7); print "bzip2-" $7; exit }' 41 bzip2 --help 2>&1 | awk '{ sub(",", "", $7); print "bzip2-" $7; exit }'
42 ;; 42 ;;
43 calendar) 43 calendar)
44 v=$(calendar -v 2>/dev/null || true) 44 v=$(calendar -v 2>/dev/null || true)
45 case "${v}" in 45 case "${v}" in
46 "") echo "calendar-20150701" ;; 46 "") echo "calendar-20150701" ;;
47 *) echo ${v} ;; 47 *) echo ${v} ;;
48 esac 48 esac
49 ;; 49 ;;
50 ftpd) 50 ftpd)
51 strings -a /usr/libexec/ftpd | awk '$1 == "NetBSD-ftpd" { print "ftpd-" $2 }' 51 strings -a /usr/libexec/ftpd | awk '$1 == "NetBSD-ftpd" { print "ftpd-" $2 }'
52 ;; 52 ;;
53 g++|c++) 53 g++|c++)
54 g++ --version | awk '{ print $1 "-" $4; exit }' 54 g++ --version | awk '{ print $1 "-" $4; exit }'
55 ;; 55 ;;
56 gcc|cc) 56 gcc|cc)
57 gcc --version | awk '{ print $1 "-" $4; exit }' 57 gcc --version | awk '{ print $1 "-" $4; exit }'
58 ;; 58 ;;
59 grep) 59 grep)
60 grep --version | awk '{ print $1 "-" $4 $5; exit }' 60 grep --version | awk '{ print $1 "-" $4 $5; exit }'
61 ;; 61 ;;
62 gzip) 62 gzip)
63 gzip --version 2>&1 | awk '{ print $2 "-" $3 }' 63 gzip --version 2>&1 | awk '{ print $2 "-" $3 }'
64 ;; 64 ;;
65 httpd|bozohttpd) 65 httpd|bozohttpd)
66 v=$(/usr/libexec/httpd -G 2>/dev/null || true) 66 v=$(/usr/libexec/httpd -G 2>/dev/null || true)
67 case "${v}" in 67 case "${v}" in
68 "") 68 "")
69 strings -a /usr/libexec/httpd | awk -F/ '$1 == "bozohttpd" && NF == 2 { print $1 "-" $2; exit }' 69 strings -a /usr/libexec/httpd | awk -F/ '$1 == "bozohttpd" && NF == 2 { print $1 "-" $2; exit }'
70 ;; 70 ;;
71 *) 71 *)
72 echo bozohttpd-${v##*/} 72 echo bozohttpd-${v##*/}
73 ;; 73 ;;
74 esac 74 esac
75 ;; 75 ;;
76 lib*) 76 lib*)
77 dlist=$(echo ${LD_LIBRARY_PATH} | awk '{ gsub(":", " "); print }') 77 dlist=$(echo ${LD_LIBRARY_PATH} | awk '{ gsub(":", " "); print }')
78 for d in ${dlist}; do 78 for d in ${dlist}; do
79 if [ -e ${d}/$1.so ]; then 79 if [ -e ${d}/$1.so ]; then
80 ls -al ${d}/$1.so | awk '{ sub(".*/", "", $11); sub("\\.so\\.", "-", $11); print $11 }' 80 ls -al ${d}/$1.so | awk '{ sub(".*/", "", $11); sub("\\.so\\.", "-", $11); print $11 }'
81 break 81 break
82 fi 82 fi
83 done 83 done
84 ;; 84 ;;
85 netbsd) 85 netbsd)
86 uname -sr | awk '{ print $1 "-" $2 }' 86 uname -sr | awk '{ print $1 "-" $2 }'
87 ;; 87 ;;
88 netpgp) 88 netpgp)
89 netpgp -V | awk '{ sub("/.*", "", $3); print "netpgp-" $3; exit }' 89 netpgp -V | awk '{ sub("/.*", "", $3); print "netpgp-" $3; exit }'
90 ;; 90 ;;
91 netpgpverify) 91 netpgpverify)
92 netpgpverify -v | awk '{ print $1 "-" $3 }' 92 netpgpverify -v | awk '{ print $1 "-" $3 }'
93 ;; 93 ;;
94 ntp) 94 ntp)
95 ntpq --version | awk '{ sub("-.", ""); sub("p", "pl", $2); print "ntp-" $2 }' 95 ntpq --version | awk '{ sub("-.", ""); sub("p", "pl", $2); print "ntp-" $2 }'
96 ;; 96 ;;
97 openssl) 97 openssl)
98 openssl version 2>/dev/null | awk '{ print tolower($1) "-" $2 }' 98 openssl version 2>/dev/null | awk '{ print tolower($1) "-" $2 }'
99 ;; 99 ;;
100 sqlite|sqlite3) 100 sqlite|sqlite3)
101 sqlite3 --version | awk '{ print "sqlite3-" $1 }' 101 sqlite3 --version | awk '{ print "sqlite3-" $1 }'
102 ;; 102 ;;
103 ssh|openssh) 103 ssh|openssh)
104 ssh -V 2>&1 | awk '{ sub("_", "-", $1); print tolower($1) }' 104 ssh -V 2>&1 | awk '{ sub("_", "-", $1); print tolower($1) }'
105 ;; 105 ;;
106 sshd) 106 sshd)
107 sshd -V 2>&1 | awk '/OpenSSH/ { sub("_", "D-", $1); print tolower($1) }' 107 sshd -V 2>&1 | awk '/OpenSSH/ { sub("_", "D-", $1); print tolower($1) }'
108 ;; 108 ;;
109 tcsh) 109 tcsh)
110 grep '/tcsh' /etc/shells > /dev/null 2>&1 && tcsh -c 'echo $version' | awk '{ print $1 "-" $2 }' 110 grep '/tcsh' /etc/shells > /dev/null 2>&1 && tcsh -c 'echo $version' | awk '{ print $1 "-" $2 }'
111 ;; 111 ;;
112 unbound) 112 unbound)
113 case $(uname -s) in 113 case $(uname -s) in
114 FreeBSD) 114 FreeBSD)
115 unbound-control -h | awk '/^Version/ { print "unbound-" $2 }' 115 unbound-control -h | awk '/^Version/ { print "unbound-" $2 }'
116 ;; 116 ;;
117 esac 117 esac
118 ;; 118 ;;
119 xz) 119 xz)
120 xz --version | awk '{ print $1 "-" $4; exit }' 120 xz --version | awk '{ print $1 "-" $4; exit }'
121 ;; 121 ;;
122 esac 122 esac
123} 123}
124 124
125case $# in 125all=false
1260) all=true ;; 126while getopts "av" a; do
127*) all=false ;; 127 case "${a}" in
128esac 128 a) all=true ;;
129 129 v) set -x ;;
130while [ $# -gt 0 ]; do 
131 case "$1" in 
132 -a) all=true ;; 
133 -v) set -x ;; 
134 *) break ;; 130 *) break ;;
135 esac 131 esac
136 shift 132 shift
137done 133done
138 134
 135# if no arg specified, we want them all
 136if [ $# -eq 0 ]; then
 137 all=true
 138fi
 139
139# if we want to do every one, then let's get the arguments 140# if we want to do every one, then let's get the arguments
140# not really scalable 141# not really scalable
141if ${all}; then 142if ${all}; then
142 args='awk bind bzip2 calendar ftpd g++ gcc grep gzip httpd netbsd netpgp' 143 args='awk bind bzip2 calendar ftpd g++ gcc grep gzip httpd netbsd netpgp'
143 args="${args} netpgpverify ntp openssl sqlite ssh sshd tcsh unbound xz" 144 args="${args} netpgpverify ntp openssl sqlite ssh sshd tcsh unbound xz"
144 set -- ${args} 145 set -- ${args}
145fi 146fi
146 147
147while [ $# -gt 0 ]; do 148while [ $# -gt 0 ]; do
148 getversion $1 149 getversion $1
149 shift 150 shift
150done 151done