Skip to contentSkip to conversation

Covscan: Shell script fixes

PATCH Applied Phil Sutter 3 participants 7 messages Aug 17, 2017 – Aug 18, 2017

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH v2 1/2 examples: Some shell fixes to cbq.init

In reply to Phil Sutter

This addresses the following issues:

- $@ is an array, so don't use it in quoted strings - use $* instead.

- Add missing quotes to components of [ ] expressions. These are not strictly necessary since the output of 'wc -l' should be a single word only, but in case of errors, bash prints "integer expression expected" instead of "too many arguments".

- Use -print0/-0 when piping from find to xargs to allow for filenames which contain whitespace.

- Quote arguments to 'eval' to prevent word-splitting.

---
 examples/cbq.init-v0.7.3 | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
examples/cbq.init-v0.7.3 +12 -12
index 1bc0d446f8983..66448d88f0053 100644
@@ -532,7 +532,7 @@ cbq_off () {
 
 ### Prefixed message
 cbq_message () {
-	echo -e "**CBQ: $@"
+	echo -e "**CBQ: $*"
 } # cbq_message
 
 ### Failure message
@@ -560,15 +560,15 @@ cbq_time2abs () {
 ### Display CBQ setup
 cbq_show () {
 	for dev in `cbq_device_list`; do
-		[ `tc qdisc show dev $dev| wc -l` -eq 0 ] && continue
+		[ "`tc qdisc show dev $dev| wc -l`" -eq 0 ] && continue
 		echo -e "### $dev: queueing disciplines\n"
 		tc $1 qdisc show dev $dev; echo
 
-		[ `tc class show dev $dev| wc -l` -eq 0 ] && continue
+		[ "`tc class show dev $dev| wc -l`" -eq 0 ] && continue
 		echo -e "### $dev: traffic classes\n"
 		tc $1 class show dev $dev; echo
 
-		[ `tc filter show dev $dev| wc -l` -eq 0 ] && continue
+		[ "`tc filter show dev $dev| wc -l`" -eq 0 ] && continue
 		echo -e "### $dev: filtering rules\n"
 		tc $1 filter show dev $dev; echo
 	done
@@ -585,7 +585,7 @@ cbq_init () {
 
 	### Gather all DEVICE fields from $1/cbq-*
 	DEVFIELDS=`find $1 -maxdepth 1 \( -type f -or -type l \) -name 'cbq-*' \
-		  -not -name '*~' | xargs sed -n 's/#.*//; \
+		  -not -name '*~' -print0 | xargs -0 sed -n 's/#.*//; \
 		  s/[[:space:]]//g; /^DEVICE=[^,]*,[^,]*\(,[^,]*\)\?/ \
 		  { s/.*=//; p; }'| sort -u`
 	[ -z "$DEVFIELDS" ] &&
@@ -593,7 +593,7 @@ cbq_init () {
 
 	### Check for different DEVICE fields for the same device
 	DEVICES=`echo "$DEVFIELDS"| sed 's/,.*//'| sort -u`
-	[ `echo "$DEVICES"| wc -l` -ne `echo "$DEVFIELDS"| wc -l` ] &&
+	[ "`echo "$DEVICES"| wc -l`" -ne "`echo "$DEVFIELDS"| wc -l`" ] &&
 		cbq_failure "different DEVICE fields for single device!\n$DEVFIELDS"
 } # cbq_init
 
@@ -618,7 +618,7 @@ cbq_load_class () {
 	PRIO_MARK=$PRIO_MARK_DEFAULT
 	PRIO_REALM=$PRIO_REALM_DEFAULT
 
-	eval `echo "$CFILE"| grep -E "^($CBQ_WORDS)="`
+	eval "`echo "$CFILE"| grep -E "^($CBQ_WORDS)="`"
 
 	### Require RATE/WEIGHT
 	[ -z "$RATE" -o -z "$WEIGHT" ] &&
@@ -661,7 +661,7 @@ if [ "$1" = "compile" ]; then
 
 	### echo-only version of "tc" command
 	tc () {
-		echo "$TC $@"
+		echo "$TC $*"
 	} # tc
 
 elif [ -n "$CBQ_DEBUG" ]; then
@@ -669,13 +669,13 @@ elif [ -n "$CBQ_DEBUG" ]; then
 
 	### Logging version of "ip" command
 	ip () {
-		echo -e "\n# ip $@" >> $CBQ_DEBUG
+		echo -e "\n# ip $*" >> $CBQ_DEBUG
 		$IP "$@" 2>&1 | tee -a $CBQ_DEBUG
 	} # ip
 
 	### Logging version of "tc" command
 	tc () {
-		echo -e "\n# tc $@" >> $CBQ_DEBUG
+		echo -e "\n# tc $*" >> $CBQ_DEBUG
 		$TC "$@" 2>&1 | tee -a $CBQ_DEBUG
 	} # tc
 else
@@ -711,8 +711,8 @@ if [ "$1" != "compile" -a "$2" != "nocache" -a -z "$CBQ_DEBUG" ]; then
 	### validate the cache
 	[ "$2" = "invalidate" -o ! -f $CBQ_CACHE ] && VALID=0
 	if [ $VALID -eq 1 ]; then
-		[ `find $CBQ_PATH -maxdepth 1 -newer $CBQ_CACHE| \
-		  wc -l` -gt 0 ] && VALID=0
+		[ "`find $CBQ_PATH -maxdepth 1 -newer $CBQ_CACHE| \
+		  wc -l`" -gt 0 ] && VALID=0
 	fi
 
 	### compile the config if the cache is invalid
Signature · 1 line
2.13.1
#0 lore raw

v2 cover Covscan: Shell script fixes

This series collects patches from v1 which deal with programming mistakes in shell scripts.

No changes to the actual patches, just splitting into smaller series.

Phil Sutter (2):
examples: Some shell fixes to cbq.init
ifcfg: Quote left-hand side of [ ] expression

 examples/cbq.init-v0.7.3 | 24 ++++++++++++------------
 ip/ifcfg                 |  2 +-
 2 files changed, 13 insertions(+), 13 deletions(-)
Signature · 1 line
2.13.1
#2 lore raw

PATCH v2 2/2 ifcfg: Quote left-hand side of [ ] expression

This prevents word-splitting and therefore leads to more accurate error message in case 'grep -c' prints something other than a number.

---
 ip/ifcfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/ifcfg +1 -1
index 083d9df36742f..30a2dc49816cd 100644
@@ -131,7 +131,7 @@ noarp=$?
 
 ip route add unreachable 224.0.0.0/24 >& /dev/null
 ip route add unreachable 255.255.255.255 >& /dev/null
-if [ `ip link ls $dev | grep -c MULTICAST` -ge 1 ]; then
+if [ "`ip link ls $dev | grep -c MULTICAST`" -ge 1 ]; then
   ip route add 224.0.0.0/4 dev $dev scope global >& /dev/null
 fi
 
Signature · 1 line
2.13.1
#3 lore raw

RE: [iproute PATCH v2 2/2] ifcfg: Quote left-hand side of [ ] expression

From: Phil Sutter

Quoted 14 lines

Sent: 17 August 2017 18:10
This prevents word-splitting and therefore leads to more accurate error message in case 'grep -c' prints something other than a number.

---
 ip/ifcfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/ifcfg +0 -0
index 083d9df36742f..30a2dc49816cd 100644

ip/ifcfg noarp=$? · line 134

Quoted 5 lines
ip/ifcfg +1 -1
@@ -131,7 +131,7 @@ noarp=$?
 
 ip route add unreachable 224.0.0.0/24 >& /dev/null
 ip route add unreachable 255.255.255.255 >& /dev/null
-if [ `ip link ls $dev | grep -c MULTICAST` -ge 1 ]; then
+if [ "`ip link ls $dev | grep -c MULTICAST`" -ge 1 ]; then

You could drag all these scripts into the 1990's by using $(...) instead of ....

David

#4 lore raw

Re: [iproute PATCH v2 2/2] ifcfg: Quote left-hand side of [ ] expression

Quoted 16 lines · David Laight

From: Phil Sutter

Quoted 15 lines

Sent: 17 August 2017 18:10
This prevents word-splitting and therefore leads to more accurate error message in case 'grep -c' prints something other than a number.

---
 ip/ifcfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/ifcfg +0 -0
index 083d9df36742f..30a2dc49816cd 100644
@@ -131,7 +131,7 @@ noarp=$?
 
Quoted 7 lines

ip/ifcfg noarp=$? · line 134

Quoted 4 lines
ip/ifcfg +1 -1
 ip route add unreachable 224.0.0.0/24 >& /dev/null
 ip route add unreachable 255.255.255.255 >& /dev/null
-if [ `ip link ls $dev | grep -c MULTICAST` -ge 1 ]; then
+if [ "`ip link ls $dev | grep -c MULTICAST`" -ge 1 ]; then

You could drag all these scripts into the 1990's by using $(...) instead of ....

That's a different kettle of fish IMO, using $() doesn't change the situation this addresses:

| $ [ $(echo foo bar) -eq 0 ] && echo ok
| bash: [: too many arguments
| $ [ "$(echo foo bar)" -eq 0 ] && echo ok
| bash: [: foo bar: integer expression expected

Cheers, Phil

#6 lore raw
In reply to Phil Sutter
Quoted 5 lines · On Thu, 17 Aug 2017 19:09:30 +0200 Phil Sutter <[email protected]> wrote:

This series collects patches from v1 which deal with programming mistakes in shell scripts.

No changes to the actual patches, just splitting into smaller series.

Quoted 8 lines

Phil Sutter (2):
examples: Some shell fixes to cbq.init
ifcfg: Quote left-hand side of [ ] expression

 examples/cbq.init-v0.7.3 | 24 ++++++++++++------------
 ip/ifcfg                 |  2 +-
 2 files changed, 13 insertions(+), 13 deletions(-)

Applied.

#5 lore raw

RE: [iproute PATCH v2 2/2] ifcfg: Quote left-hand side of [ ] expression

In reply to Phil Sutter

From: Phil Sutter

Quoted 1 line

Sent: 18 August 2017 12:24

...

Quoted 6 lines
Quoted 5 lines

ip/ifcfg noarp=$? · line 134

Quoted 2 lines
ip/ifcfg +1 -1
-if [ `ip link ls $dev | grep -c MULTICAST` -ge 1 ]; then
+if [ "`ip link ls $dev | grep -c MULTICAST`" -ge 1 ]; then

You could drag all these scripts into the 1990's by using $(...) instead of ....

Quoted 7 lines

That's a different kettle of fish IMO, using $() doesn't change the situation this addresses:

| $ [ $(echo foo bar) -eq 0 ] && echo ok
| bash: [: too many arguments
| $ [ "$(echo foo bar)" -eq 0 ] && echo ok
| bash: [: foo bar: integer expression expected

I didn't say it would.
IFS=
set -o noglob
would though - not that I'd suggest it here.
protecting the 'eval' is somewhat harder.

David