Browse Source

Code linting with shellcheck

Yann Weber 11 months ago
parent
commit
18fff4b947
2 changed files with 69 additions and 70 deletions
  1. 65
    67
      check.sh
  2. 4
    3
      example.sh

+ 65
- 67
check.sh View File

@@ -45,12 +45,12 @@ col_set() {
45 45
 	fi
46 46
 	if [ "$color" -gt 0 ]
47 47
 	then
48
-		tput setaf $1
48
+		tput setaf "$1"
49 49
 	fi
50 50
 }
51 51
 
52 52
 log() {
53
-	echo "$(col_set $3)[$(printf "%7s" "$1")]$(col_reset) $2"
53
+	echo "$(col_set "$3")[$(printf "%7s" "$1")]$(col_reset) $2"
54 54
 }
55 55
 
56 56
 datetime() {
@@ -58,7 +58,7 @@ datetime() {
58 58
 }
59 59
 
60 60
 logdate() {
61
-	echo "$(date -Iseconds) $(col_set $3)[$(printf "%7s" "$1")]$(col_reset) $2"
61
+	echo "$(date -Iseconds) $(col_set "$3")[$(printf "%7s" "$1")]$(col_reset) $2"
62 62
 }
63 63
 
64 64
 fail() {
@@ -150,7 +150,6 @@ TC_RUN() {
150 150
 	_test_setup
151 151
 	cmd=$1
152 152
 	shift
153
-	args=""
154 153
 	case $#
155 154
 	in
156 155
 		1)$cmd "$1";;
@@ -159,10 +158,9 @@ TC_RUN() {
159 158
 		4)$cmd "$1" "$2" "$3" "$4";;
160 159
 		*)fail "To many arguments for $cmd"
161 160
 	esac
162
-	#$cmd $args
163 161
 
164
-	tc_run=$(expr $tc_run + 1)
165
-	total_run=$(expr $total_run + 1)
162
+	tc_run=$(( tc_run + 1))
163
+	total_run=$(( total_run + 1))
166 164
 	if [ "$test_status" -ne 0 ]
167 165
 	then
168 166
 		err_type="FAIL"
@@ -170,14 +168,14 @@ TC_RUN() {
170 168
 		then
171 169
 			err_type="ERR"
172 170
 		fi
173
-		tc_fail=$(expr $tc_fail + 1)
174
-		total_fail=$(expr $total_fail + 1)
171
+		tc_fail=$(( tc_fail + 1))
172
+		total_fail=$(( total_fail + 1))
175 173
 		logdate $err_type "$tc_name: $test_msg" 1 >&2
176 174
 	elif [ "$verbose" -gt 1 ]
177 175
 	then
178 176
 		logdate OK "$tc_name: $test_msg" 2
179 177
 	else
180
-		echo -n '.'
178
+		printf '.'
181 179
 	fi
182 180
 }
183 181
 
@@ -186,7 +184,7 @@ CHECK_START() {
186 184
 }
187 185
 
188 186
 CHECK_REPORT() {
189
-	if [ "$verbose" -eq 0 -a "$tc_fail" -eq "0" ]
187
+	if [ "$verbose" -eq 0 ] && [ "$tc_fail" -eq "0" ]
190 188
 	then
191 189
 		echo "" # for dot printed with -n
192 190
 	fi
@@ -221,7 +219,7 @@ _check_http_status() {
221 219
 	fi
222 220
 	shift 1
223 221
 
224
-	status=$(curl -s -o /dev/null -w "%{http_code}" $* $url)
222
+	status=$(curl -s -o /dev/null -w "%{http_code}" "$@" "$url")
225 223
 	if [ "$status" -ne "$expt" ]
226 224
 	then
227 225
 		fail "Check http status $expt for $url : $status returned" 
@@ -234,24 +232,24 @@ _check_http_status() {
234 232
 check_http_status() {
235 233
 	# $1 url
236 234
 	# $2 status
237
-	_check_http_status $1 $2
235
+	_check_http_status "$1" "$2"
238 236
 }
239 237
 
240 238
 check_http_200() {
241
-	_check_http_status $1 200
239
+	_check_http_status "$1" 200
242 240
 }
243 241
 
244 242
 check_https_cert() {
245 243
 	# Check that SSL Cert is valid
246 244
 	# $1 URL
247
-	status=$(curl -s -o /dev/null -w "%{http_code}" https://$1)
245
+	status=$(curl -s -o /dev/null -w "%{http_code}" "https://$1")
248 246
 	rep=$?
249 247
 	if [ "$rep" -eq 0 ]
250 248
 	then
251 249
 		success "https://$1 cert verified"
252 250
 		return
253 251
 	fi
254
-	status=$(curl -k -s -o /dev/null -q "%{http_code}" https://$1)
252
+	status=$(curl -k -s -o /dev/null -q "%{http_code}" "https://$1")
255 253
 	rep=$?
256 254
 	if [ "$rep" -eq 0 ]
257 255
 	then
@@ -273,15 +271,16 @@ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
273 271
 		<xsl:template match="/">
274 272
 				<xsl:value-of select="/html/head/title"/>
275 273
 		</xsl:template>
276
-</xsl:stylesheet>' > $tmpxsl
274
+</xsl:stylesheet>' > "$tmpxsl"
277 275
 
278 276
 	tmphtml=$(mktemp -t html.XXXXXXXXX)
279 277
 
280
-	curl --silent $url > $tmphtml
281
-	title=$(xsltproc --html --novalid $tmpxsl $tmphtml 2>/dev/null)
282
-	if [ "$?" -ne "0" ]
278
+	curl --silent "$url" > "$tmphtml"
279
+	if title=$(xsltproc --html --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
283 280
 	then
284
-		title=$(xsltproc --novalid $tmpxsl $tmphtml 2>/dev/null)
281
+		:
282
+	else
283
+		title=$(xsltproc --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
285 284
 	fi
286 285
 
287 286
 	if [ "$title" = "$expt" ]
@@ -291,7 +290,7 @@ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
291 290
 		fail "$url HTML title is '$title' but '$expt' expected"
292 291
 	fi
293 292
 
294
-	rm $tmpxsl $tmphtml 2>/dev/null
293
+	rm "$tmpxsl" "$tmphtml" 2>/dev/null
295 294
 }
296 295
 
297 296
 check_audiostream() {
@@ -299,12 +298,12 @@ check_audiostream() {
299 298
 	# and attempt to decode it.
300 299
 	# $1 Stream URL
301 300
 
302
-	MPLAYER="`which mplayer`"
303
-	MPV="`which mpv`"
304
-	if [ -x $MPLAYER ]
301
+	MPLAYER="$(which mplayer)"
302
+	MPV="$(which mpv)"
303
+	if [ -x "$MPLAYER" ]
305 304
 	then
306 305
 		check_audiostream_mplayer "$1"
307
-	elif [ -x $MPV ]
306
+	elif [ -x "$MPV" ]
308 307
 	then
309 308
 		check_audiostream_mpv "$1"
310 309
 	else
@@ -327,21 +326,21 @@ check_audiostream_mplayer() {
327 326
 	MPLAYER="$3"
328 327
 	if [ -z "$MPLAYER" ]
329 328
 	then
330
-		MPLAYER="`which mplayer`"
329
+		MPLAYER="$(which mplayer)"
331 330
 	fi
332 331
 
333 332
 	sz_kb="${sz}kb"
334
-	expt_sz="`expr $sz \* 8`"
333
+	expt_sz="$(( sz * 8 ))"
335 334
 
336 335
 	if [ "$verbose" -gt 1 ]
337 336
 	then
338 337
 		logdate INFO "$tc_name: Running mplayer on '$1' for $sz_kb" 3
339 338
 	fi
340 339
 
341
-	$MPLAYER -endpos $sz_kb -ao pcm:file=$tmpfile "$1" 1>/dev/null 2>/dev/null
342
-	res=$?
343
-	bytes=$(du -b $tmpfile | cut -f1)
344
-	rm $tmpfile 2>/dev/null
340
+	$MPLAYER -endpos "$sz_kb" -ao "pcm:file=$tmpfile" "$1" 1>/dev/null 2>/dev/null
341
+	res="$?"
342
+	bytes=$(du -b "$tmpfile" | cut -f1)
343
+	rm "$tmpfile" 2>/dev/null
345 344
 
346 345
 	if [ "$bytes" -lt $expt_sz ]
347 346
 	then
@@ -370,7 +369,7 @@ check_audiostream_mpv() {
370 369
 	MPV="$3"
371 370
 	if [ -z "$MPV" ]
372 371
 	then
373
-		MPV="`which mpv`"
372
+		MPV="$(which mpv)"
374 373
 	fi
375 374
 
376 375
 
@@ -378,19 +377,19 @@ check_audiostream_mpv() {
378 377
 	then
379 378
 		logdate INFO "$tc_name: Running mpv on '$1' for ${time}s" 3
380 379
 	fi
381
-	start_time=`date "+%s"`
380
+	start_time=$(date "+%s")
382 381
 	$MPV --vo=null --ao=null --o=/dev/null --of=wav --length $time "$1" 1> /dev/null 2>/dev/null
383 382
 	res=$?
384
-	stop_time=`date "+%s"`
385
-	run_time=`expr $stop_time - $start_time`
383
+	stop_time=$(date "+%s")
384
+	run_time=$(( stop_time - start_time))
386 385
 
387
-	if [ $run_time -lt $time ]
386
+	if [ "$run_time" -lt "$time" ]
388 387
 	then
389 388
 		fail "mpv stopped running ${run_time} after launch but ${time}s should be received"
390 389
 		return
391 390
 	fi
392 391
 	
393
-	if [ $res -eq 0 ]
392
+	if [ "$res" -eq 0 ]
394 393
 	then
395 394
 		success "mpv retrieved ${time}s of stream on $1"
396 395
 	else
@@ -399,7 +398,7 @@ check_audiostream_mpv() {
399 398
 }
400 399
 
401 400
 check_ping() {
402
-	ns=$1
401
+	hostname=$1
403 402
 	count=$2
404 403
 	proto="$3"
405 404
 	if [ -z "$count" ]
@@ -408,19 +407,18 @@ check_ping() {
408 407
 	fi
409 408
 	case "$proto" in
410 409
 		ipv4)
411
-			proto="-4";;
410
+			protoping="ping -4";;
412 411
 		ipv6)
413
-			proto="-6";;
412
+			protoping="ping -6";;
414 413
 		*)
415
-			proto="";;
414
+			protoping="ping";;
416 415
 	esac
417
-	ping $proto -i 0.2 -c $count $ns 2>/dev/null >/dev/null
418
-	res=$?
419
-	if [ "$res" -ne 0 ]
416
+
417
+	if $protoping -i 0.2 -c "$count" "$hostname" >/dev/null
420 418
 	then
421
-		fail "unable to ping '$ns'"
419
+		success "successfully send $count ping to '$hostname'"
422 420
 	else
423
-		success "successfully send $count ping to '$ns'"
421
+		fail "unable to ping '$hostname'"
424 422
 	fi
425 423
 }
426 424
 
@@ -489,9 +487,9 @@ check_imaps() {
489 487
 
490 488
 check_git_repo() {
491 489
 	tmpdir=$(mktemp -d -t check_git.XXXXXXXXX)
492
-	git clone $1 $tmpdir 2>/dev/null 1>/dev/null
493
-	res=$?
494
-	rm -Rf $tmpdir
490
+	git clone "$1" "$tmpdir" 2>/dev/null 1>/dev/null
491
+	res="$?"
492
+	rm -Rf "$tmpdir"
495 493
 	if [ "$res" -ne 0 ]
496 494
 	then
497 495
 		fail "unable to clone git repo '$1'"
@@ -509,13 +507,13 @@ __xmpp_probe() {
509 507
 	timeout=$2
510 508
 	type=$3
511 509
 
512
-	payload="<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n"
510
+	payload=$(printf "%s" "<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n")
513 511
 	if [ "$verbose" -gt 2 ]
514 512
 	then
515
-		echo -e $payload | sed -e "s/^/$(datetime) [  DEBUG] Sent : /" >&2
513
+		echo "$payload" | sed -e "s/^/$(datetime) [  DEBUG] Sent : /" >&2
516 514
 	fi
517
-	echo -e $payload
518
-	sleep $timeout
515
+	echo "$payload"
516
+	sleep "$timeout"
519 517
 }
520 518
 
521 519
 _xmpp_probe() {
@@ -526,21 +524,21 @@ _xmpp_probe() {
526 524
 
527 525
 	tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
528 526
 
529
-	echo "$(__xmpp_probe $serv $timeout $type| nc -q2 $serv $port)</stream:stream>" | xmllint --format - > $tmpres
527
+	echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc -q2 "$serv" "$port")</stream:stream>" | xmllint --format - > "$tmpres"
530 528
 	if [ "$verbose" -gt 2 ]
531 529
 	then
532
-		cat $tmpres | sed -e "s/^/$(datetime) [  DEBUG] Recv : /" >&2
530
+		sed -e "s/^/$(datetime) [  DEBUG] Recv : /" >&2 < "$tmpres"
533 531
 	fi
534
-	cat $tmpres
535
-	rm $tmpres
532
+	cat "$tmpres"
533
+	rm "$tmpres"
536 534
 }
537 535
 
538 536
 _check_xmpp_ns() {
539 537
 	ns1=$1
540 538
 	expt_ns=$2
541 539
 	expt_port=$3
542
-	dnsq="_xmpp-${4}._tcp.$1"
543
-	rep="$(dig $dnsq srv +short | cut -d" " -f3,4)"
540
+	dnsq="_xmpp-${4}._tcp.${ns1}"
541
+	rep="$(dig "$dnsq" srv +short | cut -d" " -f3,4)"
544 542
 	if [ "$rep" = "$expt_port ${expt_ns}." ]
545 543
 	then
546 544
 		success "$dnsq = '$rep'"
@@ -582,7 +580,7 @@ _check_xmpp_server() {
582 580
 		logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port (timeout=${timeout}s)" 3
583 581
 	fi
584 582
 
585
-	stream=$(_xmpp_probe $serv $port $timeout $type| head -n2 | tail -n1)
583
+	stream=$(_xmpp_probe "$serv" "$port" "$timeout" "$type"| head -n2 | tail -n1)
586 584
 	if [ -z "$stream" ]
587 585
 	then
588 586
 		fail "Empty reply from $serv:$port"
@@ -591,9 +589,9 @@ _check_xmpp_server() {
591 589
 
592 590
 	if [ "$type" = "client" ]
593 591
 	then
594
-		infos=$(echo $stream | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
592
+		infos=$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
595 593
 	else
596
-		infos="$(echo $stream | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
594
+		infos="$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
597 595
 	fi
598 596
 
599 597
 	if echo "$infos" | grep "jabber:$type" >/dev/null
@@ -621,7 +619,7 @@ check_xmpp_ssl() {
621 619
 		port=5222
622 620
 	fi
623 621
 
624
-	openssl s_client -connect $serv:$port </dev/null -starttls xmpp >/dev/null 2>/dev/null
622
+	openssl s_client -connect "$serv:$port" </dev/null -starttls xmpp >/dev/null 2>/dev/null
625 623
 	rep=$?
626 624
 	if [ "$rep" -eq 0 ]
627 625
 	then
@@ -644,14 +642,14 @@ check_ssh_nc() {
644 642
 		port=22
645 643
 	fi
646 644
 
647
-	rep="$(nc -w1 $host $port </dev/null)"
645
+	rep="$(nc -w1 "$host" "$port" </dev/null)"
648 646
 	res=$?
649 647
 	if [ "$res" -ne "0" ]
650 648
 	then
651 649
 		fail "Netcat unable to connect to $host:$port"
652 650
 		return
653 651
 	fi
654
-	if echo $rep | grep "^SSH-2.0-OpenSSH" >/dev/null
652
+	if echo "$rep" | grep "^SSH-2.0-OpenSSH" >/dev/null
655 653
 	then
656 654
 		success "OpenSSH replied on $host:$port"
657 655
 	else
@@ -675,7 +673,7 @@ check_ssh_key() {
675 673
 		keytype="rsa"
676 674
 	fi
677 675
 
678
-	key=$(ssh-keyscan -p $port -t $keytype $host 2>/dev/null | cut -d " " -f3)
676
+	key=$(ssh-keyscan -p $port -t "$keytype" "$host" 2>/dev/null | cut -d " " -f3)
679 677
 
680 678
 	if [ -z "$key" ]
681 679
 	then

+ 4
- 3
example.sh View File

@@ -12,9 +12,10 @@ verbose=0
12 12
 exit_on_fail=1
13 13
 color=1
14 14
 
15
-OPTS=$(getopt -o vcnh --long verbose,continue,no-color,help: -n 'parse-options' -- "$@")
16
-if [ "$?" -ne "0" ]
15
+if OPTS=$(getopt -o vcnh --long verbose,continue,no-color,help: -n 'parse-options' -- "$@")
17 16
 then
17
+	:
18
+else
18 19
 	echo ""
19 20
 	usage
20 21
 	exit 1
@@ -24,7 +25,7 @@ for o in $OPTS
24 25
 do
25 26
 	case "$o"
26 27
 	in
27
-		-v | --verbose ) verbose=$(expr $verbose + 1);;
28
+		-v | --verbose ) verbose=$(( verbose + 1 ));;
28 29
 		-c | --continue ) exit_on_fail=0;;
29 30
 		-n | --no-color ) color=0;;
30 31
 		-h | --help ) usage; exit 0;;

Loading…
Cancel
Save