"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Christian Weisgerber <naddy@mips.inka.de>
Subject:
regress common.sh: POSIX arithmetic for trim_obj_id()
To:
gameoftrees@openbsd.org
Date:
Wed, 16 Sep 2020 19:37:01 +0200

Download raw body.

Thread
Replace ksh syntax with POSIX arithmetic expressions.

This one had me puzzled.  How did the while condition ever work?
Apparently, variables are expanded without '$' for arithmetic(!)
comparisons inside [[ ... ]] and in our (k)sh this behavior accidentally
leaks out to [ ... ].

Mark the variables as "local" while here.  Actually, "local" isn't
part of the POSIX shell language, but it's used pervasively in these
scripts and seems widely supported in practice.

ok?

diff 0429cd76586cecb81d322546ab686ce527eb8f83 /home/naddy/got
blob - e95df3908f366957e8608730cd2dc49e62155ac0
file + regress/cmdline/common.sh
--- regress/cmdline/common.sh
+++ regress/cmdline/common.sh
@@ -96,13 +96,13 @@ git_show_tree()
 
 trim_obj_id()
 {
-	let trimcount=$1
-	id=$2
+	local trimcount=$1
+	local id=$2
 
-	pat=""
-	while [ trimcount -gt 0 ]; do
+	local pat=""
+	while [ "$trimcount" -gt 0 ]; do
 		pat="[0-9a-f]$pat"
-		let trimcount--
+		trimcount=$((trimcount - 1))
 	done
 
 	echo ${id%$pat}
-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de