On 06/28/2010 04:19 PM, Ralf Wildenhues wrote:
> Hi Eric,
> 
> thanks for the suggestion.  I had considered the idea for a second, but
> failed to see the nontrivial half.
> 
> * Eric Blake wrote on Mon, Jun 28, 2010 at 02:49:40PM CEST:
>> tmp=${1#?}
>> patt=
>> i=2
>> while test $i -lt ${#1}; do
>>   patt="?$patt"
> 
> If the parameter may be expected to be very long (which I don't think it
> will be) then
>   func_append patt '?'
> 
> would be useful here, and even if not, appending rather than prepending
> here helps with current bash.
> 
>>   i=$((i+1))
>> done
>> result=${tmp%$patt}

After thinking a bit more, this alternative should do the same job in
fewer lines of code and fewer temporary variables:

result=${1#?}
while test ${#result} -gt 1; do
  result=${result%?}
done

Also, you have to be a bit careful:
$ tmp=a
$ echo ${tmp:2}

$ echo ${tmp#??}
a

that is, before trying to split off the first two bytes using XSI, you
must ensure that ${#1} has at least two bytes to be split in the first
place.

-- 
Eric Blake   ebl...@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to