xfsqa: more sh to bash conversions
[xfstests-dev.git] / common
1 ##/bin/bash 
2 #
3 # Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it would be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # common procedures for QA scripts
20 #
21 # $Header: /disk7/depot/linux/pcp/dev/qa/RCS/common,v 2.52 2000/04/05 18:24:51 kenmcd Exp $
22 #
23
24 _setenvironment()
25 {
26     MSGVERB="text:action"
27     export MSGVERB
28 }
29
30 here=`pwd`
31 rm -f $here/$iam.out
32 _setenvironment
33
34 check=${check-true}
35
36 diff="diff -u"
37 verbose=false
38 group=false
39 xgroup=false
40 showme=false
41 sortme=false
42 expunge=true
43 have_test_arg=false
44 randomize=false
45 rm -f $tmp.list $tmp.tmp $tmp.sed
46
47 # Autodetect fs type based on what's on $TEST_DEV
48 if [ "$HOSTOS" == "Linux" ]
49 then
50     export FSTYP=`blkid -c /dev/null -s TYPE -o value $TEST_DEV`
51 else
52     export FSTYP=xfs
53 fi
54
55 for r
56 do
57
58     if $group
59     then
60         # arg after -g
61         group_list=`sed -n <group -e 's/$/ /' -e "/^[0-9][0-9][0-9].* $r /"'{
62 s/ .*//p
63 }'`
64         if [ -z "$group_list" ]
65         then
66             echo "Group \"$r\" is empty or not defined?"
67             exit 1
68         fi
69         [ ! -s $tmp.list ] && touch $tmp.list
70         for t in $group_list
71         do
72             if grep -s "^$t\$" $tmp.list >/dev/null
73             then
74                 :
75             else
76                 echo "$t" >>$tmp.list
77             fi
78         done
79         group=false
80         continue
81
82     elif $xgroup
83     then
84         # arg after -x
85         [ ! -s $tmp.list ] && ls [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] >$tmp.list 2>/dev/null
86         group_list=`sed -n <group -e 's/$/ /' -e "/^[0-9][0-9][0-9].* $r /"'{
87 s/ .*//p
88 }'`
89         if [ -z "$group_list" ]
90         then
91             echo "Group \"$r\" is empty or not defined?"
92             exit 1
93         fi
94         numsed=0
95         rm -f $tmp.sed
96         for t in $group_list
97         do
98             if [ $numsed -gt 100 ]
99             then
100                 sed -f $tmp.sed <$tmp.list >$tmp.tmp
101                 mv $tmp.tmp $tmp.list
102                 numsed=0
103                 rm -f $tmp.sed
104             fi
105             echo "/^$t\$/d" >>$tmp.sed
106             numsed=`expr $numsed + 1`
107         done
108         sed -f $tmp.sed <$tmp.list >$tmp.tmp
109         mv $tmp.tmp $tmp.list
110         xgroup=false
111         continue
112     fi
113
114     xpand=true
115     case "$r"
116     in
117
118         -\? | -h | --help)      # usage
119             echo "Usage: $0 [options] [testlist]"'
120
121 common options
122     -v                  verbose
123
124 check options
125     -xfs                test XFS (default)
126     -udf                test UDF
127     -nfs                test NFS
128     -l                  line mode diff
129     -xdiff              graphical mode diff
130     -udiff              show unified diff (default)
131     -n                  show me, do not run tests
132     -q                  quick [deprecated]
133     -T                  output timestamps
134     -r                  randomize test order
135     
136 testlist options
137     -g group[,group...] include tests from these groups
138     -x group[,group...] exclude tests from these groups
139     NNN                 include test NNN
140     NNN-NNN             include test range (eg. 012-021)
141 '
142             exit 0
143             ;;
144
145         -udf)   # -udf ... set FSTYP to udf
146             FSTYP=udf
147             xpand=false
148             ;;
149
150         -xfs)   # -xfs ... set FSTYP to xfs
151             FSTYP=xfs
152             xpand=false
153             ;;
154
155         -nfs)   # -nfs ... set FSTYP to nfs
156             FSTYP=nfs
157             xpand=false
158             ;;
159
160         -g)     # -g group ... pick from group file
161             group=true
162             xpand=false
163             ;;
164
165         -l)     # line mode for diff, was default before
166             diff="diff"
167             xpand=false
168             ;;
169
170         -xdiff) # graphical diff mode
171             xpand=false
172
173             if [ ! -z "$DISPLAY" ]
174             then
175                 which xdiff >/dev/null 2>&1 && diff=xdiff
176                 which gdiff >/dev/null 2>&1 && diff=gdiff
177                 which tkdiff >/dev/null 2>&1 && diff=tkdiff
178                 which xxdiff >/dev/null 2>&1 && diff=xxdiff
179             fi
180             ;;
181
182         -udiff) # show a unified diff, default now, keep for backward compat
183             xpand=false
184             diff="$diff -u"
185             ;;
186
187         -q)     # "quick", no longer used - always quick :-)
188             xpand=false
189             ;;
190
191         -n)     # show me, don't do it
192             showme=true
193             xpand=false
194             ;;
195         -r)     # randomize test order
196             randomize=true
197             xpand=false
198             ;;
199
200         -T)     # turn on timestamp output
201             timestamp=true
202             xpand=false
203             ;;
204
205         -v)
206             verbose=true
207             xpand=false
208             ;;
209         -x)     # -x group ... exclude from group file
210             xgroup=true
211             xpand=false
212             ;;
213         '[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
214             echo "No tests?"
215             status=1
216             exit $status
217             ;;
218
219         [0-9]*-[0-9]*)
220             eval `echo $r | sed -e 's/^/start=/' -e 's/-/ end=/'`
221             ;;
222
223         [0-9]*-)
224             eval `echo $r | sed -e 's/^/start=/' -e 's/-//'`
225             end=`echo [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] | sed -e 's/\[0-9]//g' -e 's/  *$//' -e 's/.* //'`
226             if [ -z "$end" ]
227             then
228                 echo "No tests in range \"$r\"?"
229                 status=1
230                 exit $status
231             fi
232             ;;
233
234         *)
235             start=$r
236             end=$r
237             ;;
238
239     esac
240
241     # get rid of leading 0s as can be interpreted as octal
242     start=`echo $start | sed 's/^0*//'`
243     end=`echo $end | sed 's/^0*//'`
244
245     if $xpand
246     then
247         have_test_arg=true
248         $AWK_PROG </dev/null '
249 BEGIN   { for (t='$start'; t<='$end'; t++) printf "%03d\n",t }' \
250         | while read id
251         do
252             if grep -s "^$id " group >/dev/null
253             then
254                 # in group file ... OK
255                 echo $id >>$tmp.list
256             else
257                 if [ -f expunged ] && $expunge && egrep "^$id([         ]|\$)" expunged >/dev/null
258                 then
259                     # expunged ... will be reported, but not run, later
260                     echo $id >>$tmp.list
261                 else
262                     # oops
263                     echo "$id - unknown test, ignored"
264                 fi
265             fi
266         done
267     fi
268
269 done
270
271 if [ -s $tmp.list ]
272 then
273     # found some valid test numbers ... this is good
274     :
275 else
276     if $have_test_arg
277     then
278         # had test numbers, but none in group file ... do nothing
279         touch $tmp.list
280     else
281         # no test numbers, do everything from group file
282         sed -n -e '/^[0-9][0-9][0-9]*/s/[       ].*//p' <group >$tmp.list
283     fi
284 fi
285
286 # should be sort -n, but this did not work for Linux when this
287 # was ported from IRIX
288 #
289 list=`sort $tmp.list`
290 rm -f $tmp.list $tmp.tmp $tmp.sed
291
292 if $randomize
293 then
294     list=`echo $list | awk -f randomize.awk`
295 fi
296
297 case "$FSTYP" in
298     xfs)
299          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
300          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
301          [ "$XFS_CHECK_PROG" = "" ] && _fatal "xfs_check not found"
302          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
303          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found" 
304          ;;
305     udf)
306          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
307          ;;
308     nfs)
309          ;;
310 esac