Browse Source

Code linting with shellcheck

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

+ 4
- 3
example.sh View File

12
 exit_on_fail=1
12
 exit_on_fail=1
13
 color=1
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
 then
16
 then
17
+	:
18
+else
18
 	echo ""
19
 	echo ""
19
 	usage
20
 	usage
20
 	exit 1
21
 	exit 1
24
 do
25
 do
25
 	case "$o"
26
 	case "$o"
26
 	in
27
 	in
27
-		-v | --verbose ) verbose=$(expr $verbose + 1);;
28
+		-v | --verbose ) verbose=$(( verbose + 1 ));;
28
 		-c | --continue ) exit_on_fail=0;;
29
 		-c | --continue ) exit_on_fail=0;;
29
 		-n | --no-color ) color=0;;
30
 		-n | --no-color ) color=0;;
30
 		-h | --help ) usage; exit 0;;
31
 		-h | --help ) usage; exit 0;;

Loading…
Cancel
Save