[OS-BUILD PATCH 2/20] mod-blacklist.sh: Fix syntax flagged by shellcheck

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Ben Crocker <bcrocker@xxxxxxxxxx>

SC2006: Use $(...) notation instead of legacy backticked `...`.
SC2013: To read lines rather than words, pipe/redirect to a 'while read' loop. ???
SC2086: Double quote to prevent globbing and word splitting.
SC2144: -f doesn't work with globs. Use a for loop.
SC2155: Declare and assign separately to avoid masking return values. ???
SC2162: read without -r will mangle backslashes.
SC2236: Use -z instead of ! -n.

Disable:
SC2164: Use 'pushd ... || exit' or 'pushd ... || return' in case pushd fails.

Signed-off-by: Ben Crocker <bcrocker@xxxxxxxxxx>
---
 redhat/mod-blacklist.sh | 84 +++++++++++++++++++++++------------------
 1 file changed, 47 insertions(+), 37 deletions(-)

diff --git a/redhat/mod-blacklist.sh b/redhat/mod-blacklist.sh
index f2f801b5e126..1fcccc177c92 100755
--- a/redhat/mod-blacklist.sh
+++ b/redhat/mod-blacklist.sh
@@ -1,4 +1,5 @@
 #! /bin/bash
+# shellcheck disable=SC2164
 
 RpmDir=$1
 ModDir=$2
@@ -24,9 +25,9 @@ __EOF__
 
 check_blacklist()
 {
-	mod=$(find $RpmDir/$ModDir -name "$1")
+	mod=$(find "$RpmDir/$ModDir" -name "$1")
 	[ ! "$mod" ] && return 0
-	if modinfo $mod | grep -q '^alias:\s\+net-'; then
+	if modinfo "$mod" | grep -q '^alias:\s\+net-'; then
 		mod="${1##*/}"
 		mod="${mod%.ko*}"
 		echo "$mod has an alias that allows auto-loading. Blacklisting."
@@ -37,7 +38,7 @@ check_blacklist()
 find_depends()
 {
 	dep=$1
-	depends=`modinfo $dep | sed -n -e "/^depends/ s/^depends:[ \t]*//p"`
+	depends=$(modinfo "$dep" | sed -n -e "/^depends/ s/^depends:[ \t]*//p")
 	[ -z "$depends" ] && exit
 	for mod in ${depends//,/ }
 	do
@@ -45,14 +46,14 @@ find_depends()
 		[ -z "$match" ] && continue
 		# check if the module we are looking at is in mod-* too.
 		# if so we do not need to mark the dep as required.
-		mod2=${dep##*/}  # same as `basename $dep`, but faster
+		mod2=${dep##*/}  # same as $(basename $dep), but faster
 		match2=$(grep "^$mod2" "$ListName")
 		if [ -n "$match2" ]
 		then
 			#echo $mod2 >> notreq.list
 			continue
 		fi
-		echo $mod.ko >> req.list
+		echo "$mod".ko >> req.list
 	done
 }
 
@@ -60,11 +61,11 @@ foreachp()
 {
 	P=$(nproc)
 	bgcount=0
-	while read mod; do
+	while read -r mod; do
 		$1 "$mod" &
 
 		bgcount=$((bgcount + 1))
-		if [ $bgcount -eq $P ]; then
+		if [ $bgcount -eq "$P" ]; then
 			wait -n
 			bgcount=$((bgcount - 1))
 		fi
@@ -76,12 +77,12 @@ foreachp()
 # Destination was specified on the command line
 test -n "$4" && echo "$0: Override Destination $Dest has been specified."
 
-pushd $Dir
+pushd "$Dir"
 
-OverrideDir=$(basename $List)
+OverrideDir=$(basename "$List")
 OverrideDir=${OverrideDir%.*}
 OverrideDir=${OverrideDir#*-}
-mkdir -p $OverrideDir
+mkdir -p "$OverrideDir"
 
 rm -rf modnames
 find . -name "*.ko" -type f > modnames
@@ -94,7 +95,8 @@ cp "$List" .
 
 # This variable needs to be exported because it is used in sub-script
 # executed by xargs
-export ListName=$(basename "$List")
+ListName=$(basename "$List")
+export ListName
 
 foreachp find_depends < modnames
 
@@ -102,25 +104,25 @@ sort -u req.list > req2.list
 sort -u "$ListName" > modules2.list
 join -v 1 modules2.list req2.list > modules3.list
 
-for mod in $(cat modules3.list)
+while IFS= read -r mod
 do
-  # get the path for the module
-  modpath=`grep /$mod modnames`
-  [ -z "$modpath" ] && continue
-  echo $modpath >> dep.list
-done
+    # get the path for the module
+    modpath=$(grep /"$mod" modnames)
+    [ -z "$modpath" ] && continue
+    echo "$modpath" >> dep.list
+done < modules3.list
 
 sort -u dep.list > dep2.list
 
 if [ -n "$Dest" ]; then
-	# now move the modules into the $Dest directory
-	for mod in `cat dep2.list`
-	do
-	  newpath=`dirname $mod | sed -e "s/kernel\\//$Dest\//"`
-	  mkdir -p $newpath
-	  mv $mod $newpath
-	  echo $mod | sed -e "s/kernel\\//$Dest\//" | sed -e "s|^.|${ModDir}|g" >> $RpmDir/$ListName
-	done
+    # now move the modules into the $Dest directory
+    while IFS= read -r mod
+    do
+	newpath=$(dirname "$mod" | sed -e "s/kernel\\//$Dest\//")
+	mkdir -p "$newpath"
+	mv "$mod" "$newpath"
+	echo "$mod" | sed -e "s/kernel\\//$Dest\//" | sed -e "s|^.|${ModDir}|g" >> "$RpmDir"/"$ListName"
+    done < dep2.list
 fi
 
 popd
@@ -130,31 +132,39 @@ popd
 # target doesn't try to sign a non-existent file.  This is kinda ugly, but
 # so are the modules-* packages.
 
-for mod in `cat ${Dir}/dep2.list`
+while IFS= read -r mod
 do
-  modfile=`basename $mod | sed -e 's/.ko/.mod/'`
-  rm .tmp_versions/$modfile
-done
+  modfile=$(basename "$mod" | sed -e 's/.ko/.mod/')
+  rm .tmp_versions/"$modfile"
+done < "$Dir"/dep2.list
 
-if [ ! -n "$Dest" ]; then
-	sed -e "s|^.|${ModDir}|g" ${Dir}/dep2.list > $RpmDir/$ListName
+if [ -z "$Dest" ]; then
+	sed -e "s|^.|${ModDir}|g" "$Dir"/dep2.list > "$RpmDir/$ListName"
 	echo "./$RpmDir/$ListName created."
 	[ -d "$RpmDir/etc/modprobe.d/" ] || mkdir -p "$RpmDir/etc/modprobe.d/"
-	foreachp check_blacklist < $List
+	foreachp check_blacklist < "$List"
 fi
 
 # Many BIOS-es export a PNP-id which causes the floppy driver to autoload
 # even though most modern systems don't have a 3.5" floppy driver anymore
 # this replaces the old die_floppy_die.patch which removed the PNP-id from
 # the module
-if [ -f $RpmDir/$ModDir/extra/drivers/block/floppy.ko* ]; then
-	blacklist "floppy"
-fi
+
+for f in "$RpmDir/$ModDir"/extra/drivers/block/floppy.ko*
+do
+    # This unnecessary if-stmt prevents an "unused variable"
+    # warning from shellcheck:
+    if [ -e "$f" ]
+    then
+        blacklist "floppy"
+        break
+    fi
+done
 
 # avoid an empty kernel-extra package
-echo "$ModDir/$OverrideDir" >> $RpmDir/$ListName
+echo "$ModDir/$OverrideDir" >> "$RpmDir/$ListName"
 
-pushd $Dir
+pushd "$Dir"
 rm modnames dep.list dep2.list req.list req2.list
 rm "$ListName" modules2.list modules3.list
 popd
-- 
GitLab
_______________________________________________
kernel mailing list -- kernel@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to kernel-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/kernel@xxxxxxxxxxxxxxxxxxxxxxx




[Index of Archives]     [Fedora General Discussion]     [Older Fedora Users Archive]     [Fedora Advisory Board]     [Fedora Security]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Mentors]     [Fedora Package Announce]     [Fedora Package Review]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Coolkey]     [Yum Users]     [Tux]     [Yosemite News]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [USB]     [Asterisk PBX]

  Powered by Linux