diff mbox series

[ndctl,v3,2/2] test/monitor.sh: Fix shellcheck SC2086 issues as more as possible

Message ID 20241018013020.2523845-2-lizhijian@fujitsu.com (mailing list archive)
State New
Headers show
Series [ndctl,v3,1/2] test/monitor.sh convert float to integer before increment | expand

Commit Message

Zhijian Li (Fujitsu) Oct. 18, 2024, 1:30 a.m. UTC
SC2086 [1], aka. Double quote to prevent globbing and word splitting.

Previously, SC2086 will cause error in [[]] or [], for example
$ grep -w line build/meson-logs/testlog.txt
test/monitor.sh: line 99: [: too many arguments
test/monitor.sh: line 99: [: nmem0: binary operator expected

Firstly,  generated diff by shellcheck tool:
$ shellcheck -i SC2086 -f diff test/monitor.sh

In addition, we have remove the double quote around $1 like below
changes. That's because when an empty "$1" passed to a command will open to ''
it would cause an error, for example
$ ndctl/build/test/list-smart-dimm -b nfit_test.0 ''
  Error: unknown parameter ""

-       $NDCTL monitor -c "$monitor_conf" -l "$logfile" "$1" &
+       $NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &

-       jlist=$("$TEST_PATH"/list-smart-dimm -b "$smart_supported_bus" "$1")
+       jlist=$("$TEST_PATH"/list-smart-dimm -b "$smart_supported_bus" $1)

-       $NDCTL inject-smart "$monitor_dimms" "$1"
+       $NDCTL inject-smart "$monitor_dimms" $1

-       [[ $1 == $notify_dimms ]]
+       [[ "$1" == "$notify_dimms" ]]

-               [ ! -z "$monitor_dimms" ] && break
+               [[ "$monitor_dimms" ]] && break

[1] https://www.shellcheck.net/wiki/SC2086
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
`shellcheck -i SC2086 -f diff test/*.sh | patch -p1` can auto correct some
remaining SC2086 issues, however we can find it still miss some
patterns and some changes will break the origial test.

V3:
  - Fix SC2086 issues as more as possible # Alison
  - covert [ ! -z $foo ] to [[  "foo" ]] as Vishal's suggestion.
V1:
 V1 has a mistake which overts to integer too late.
 Move the conversion forward before the operation
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
 test/monitor.sh | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)
diff mbox series

Patch

diff --git a/test/monitor.sh b/test/monitor.sh
index 7049b36..be8e24d 100755
--- a/test/monitor.sh
+++ b/test/monitor.sh
@@ -11,7 +11,7 @@  monitor_regions=""
 monitor_namespace=""
 smart_supported_bus=""
 
-. $(dirname $0)/common
+. $(dirname "$0")/common
 
 monitor_conf="$TEST_PATH/../ndctl"
 
@@ -24,51 +24,51 @@  check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
 start_monitor()
 {
 	logfile=$(mktemp)
-	$NDCTL monitor -c "$monitor_conf" -l $logfile $1 &
+	$NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &
 	monitor_pid=$!
 	sync; sleep 3
-	truncate --size 0 $logfile #remove startup log
+	truncate --size 0 "$logfile" #remove startup log
 }
 
 set_smart_supported_bus()
 {
 	smart_supported_bus=$NFIT_TEST_BUS0
-	monitor_dimms=$($TEST_PATH/list-smart-dimm -b $smart_supported_bus | jq -r .[0].dev)
-	if [ -z $monitor_dimms ]; then
+	monitor_dimms=$("$TEST_PATH"/list-smart-dimm -b "$smart_supported_bus" | jq -r .[0].dev)
+	if [ -z "$monitor_dimms" ]; then
 		smart_supported_bus=$NFIT_TEST_BUS1
 	fi
 }
 
 get_monitor_dimm()
 {
-	jlist=$($TEST_PATH/list-smart-dimm -b $smart_supported_bus $1)
-	monitor_dimms=$(jq '.[]."dev"?, ."dev"?' <<<$jlist | sort | uniq | xargs)
-	echo $monitor_dimms
+	jlist=$("$TEST_PATH"/list-smart-dimm -b "$smart_supported_bus" $1)
+	monitor_dimms=$(jq '.[]."dev"?, ."dev"?' <<<"$jlist" | sort | uniq | xargs)
+	echo "$monitor_dimms"
 }
 
 call_notify()
 {
-	$TEST_PATH/smart-notify $smart_supported_bus
+	"$TEST_PATH"/smart-notify "$smart_supported_bus"
 	sync; sleep 3
 }
 
 inject_smart()
 {
-	$NDCTL inject-smart $monitor_dimms $1
+	$NDCTL inject-smart "$monitor_dimms" $1
 	sync; sleep 3
 }
 
 check_result()
 {
-	jlog=$(cat $logfile)
-	notify_dimms=$(jq ."dimm"."dev" <<<$jlog | sort | uniq | xargs)
-	[[ $1 == $notify_dimms ]]
+	jlog=$(cat "$logfile")
+	notify_dimms=$(jq ."dimm"."dev" <<<"$jlog" | sort | uniq | xargs)
+	[[ "$1" == "$notify_dimms" ]]
 }
 
 stop_monitor()
 {
 	kill $monitor_pid
-	rm $logfile
+	rm "$logfile"
 }
 
 test_filter_dimm()
@@ -91,12 +91,12 @@  test_filter_bus()
 
 test_filter_region()
 {
-	count=$($NDCTL list -R -b $smart_supported_bus | jq -r .[].dev | wc -l)
+	count=$($NDCTL list -R -b "$smart_supported_bus" | jq -r .[].dev | wc -l)
 	i=0
-	while [ $i -lt $count ]; do
-		monitor_region=$($NDCTL list -R -b $smart_supported_bus | jq -r .[$i].dev)
+	while [ $i -lt "$count" ]; do
+		monitor_region=$($NDCTL list -R -b "$smart_supported_bus" | jq -r .[$i].dev)
 		monitor_dimms=$(get_monitor_dimm "-r $monitor_region")
-		[ ! -z $monitor_dimms ] && break
+		[[ "$monitor_dimms" ]] && break
 		i=$((i + 1))
 	done
 	start_monitor "-r $monitor_region"
@@ -108,25 +108,25 @@  test_filter_region()
 test_filter_namespace()
 {
 	reset
-	monitor_namespace=$($NDCTL create-namespace -b $smart_supported_bus | jq -r .dev)
+	monitor_namespace=$($NDCTL create-namespace -b "$smart_supported_bus" | jq -r .dev)
 	monitor_dimms=$(get_monitor_dimm "-n $monitor_namespace")
 	start_monitor "-n $monitor_namespace"
 	call_notify
 	check_result "$monitor_dimms"
 	stop_monitor
-	$NDCTL destroy-namespace $monitor_namespace -f
+	$NDCTL destroy-namespace "$monitor_namespace" -f
 }
 
 test_conf_file()
 {
 	monitor_dimms=$(get_monitor_dimm)
 	conf_file=$(mktemp)
-	echo -e "[monitor]\ndimm = $monitor_dimms" > $conf_file
+	echo -e "[monitor]\ndimm = $monitor_dimms" > "$conf_file"
 	start_monitor "-c $conf_file"
 	call_notify
 	check_result "$monitor_dimms"
 	stop_monitor
-	rm $conf_file
+	rm "$conf_file"
 }
 
 test_filter_dimmevent()
@@ -138,14 +138,14 @@  test_filter_dimmevent()
 	check_result "$monitor_dimms"
 	stop_monitor
 
-	inject_value=$($NDCTL list -H -d $monitor_dimms | jq -r .[]."health"."spares_threshold")
+	inject_value=$($NDCTL list -H -d "$monitor_dimms" | jq -r .[]."health"."spares_threshold")
 	inject_value=$((inject_value - 1))
 	start_monitor "-d $monitor_dimms -D dimm-spares-remaining"
 	inject_smart "-s $inject_value"
 	check_result "$monitor_dimms"
 	stop_monitor
 
-	inject_value=$($NDCTL list -H -d $monitor_dimms | jq -r .[]."health"."temperature_threshold")
+	inject_value=$($NDCTL list -H -d "$monitor_dimms" | jq -r .[]."health"."temperature_threshold")
 	inject_value=${inject_value%.*}
 	inject_value=$((inject_value + 1))
 	start_monitor "-d $monitor_dimms -D dimm-media-temperature"