From: Stefan Sperling Subject: Re: regress common.sh: POSIX arithmetic for trim_obj_id() To: Christian Weisgerber Cc: gameoftrees@openbsd.org Date: Wed, 16 Sep 2020 22:01:22 +0200 On Wed, Sep 16, 2020 at 07:37:01PM +0200, Christian Weisgerber wrote: > 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? Yes, ok. I think this is a case where I bashed my head against a wall until I somehow got it working, without giving any consideration to POSIX. > 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 > >