[prev in list] [next in list] [prev in thread] [next in thread]
List: busybox
Subject: Re: Bash Shell Parameter Expansion does not work as expected for ${parameter:offset:length}
From: James Hanley <jhanley () dgtlrift ! com>
Date: 2016-07-25 16:12:08
Message-ID: CAF++6ANyY2JTBdaQ+9F+dJzcMC_HtYx=B2dqM2uVgH=Ch_icBw () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Pulled from master, tested and confirmed to be fixed. Thanks.
-Jim
On Sun, Jul 24, 2016 at 9:58 PM, Denys Vlasenko <vda.linux@googlemail.com>
wrote:
> Thanks for the report. Fixed in git, please try it.
>
> On Fri, Jul 22, 2016 at 5:57 PM, James Hanley <jhanley@dgtlrift.com>
> wrote:
> > Since I'm unable to submit a bug via bugs.busybox.net due to some
> > certificate error, I'm posting it here:
> >
> >
> > When offset is a literal negative the variable produces and error "bad
> > substitution" when issuing the proper form with a space after for the
> first
> > : in ${parameter:offset:length} for a negative number. See
> >
> https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
> > subsection on ${parameter:offset:length}
> >
> > It should be noted that this does work when the offset parameter is a
> > variable instead of a literal with a negative number. The script below
> > reproduces the issue and showcases the operation with a variable offset
> vs a
> > value offset.
> >
> > #!./busybox bash
> > set -x
> >
> > parameter=abcdef
> > offset=2
> > noffset=-2
> >
> > echo "parametert '${parameter}'"
> > echo "var offset '${parameter:${offset}}'"
> > echo "var noffset '${parameter:${noffset}}'"
> > echo "val 2 '${parameter:2}'"
> > echo "val -2 (bad form) '${parameter:-2}'"
> > echo "val -2 (good form) '${parameter: -2}'"
> > #EOF
> >
> > With this script busybox bash gives the following output:
> > ~/project/busybox $ ./test.sh
> > + parameter=abcdef
> > + offset=2
> > + noffset=-2
> > + echo parametert 'abcdef'
> > parametert 'abcdef'
> > + echo var offset 'cdef'
> > var offset 'cdef'
> > + echo var noffset 'ef'
> > var noffset 'ef'
> > + echo val 2 'cdef'
> > val 2 'cdef'
> > + echo val -2 (bad form) 'abcdef'
> > val -2 (bad form) 'abcdef'
> > ./test.sh: line 13: syntax error: bad substitution
> > ~/project/busybox $
> >
> > Changing the script to use the Linux Bash:
> > jim@jim-VirtualBox:~/project/busybox$ ./test.sh
> > + parameter=abcdef
> > + offset=2
> > + noffset=-2
> > + echo 'parametert '\''abcdef'\'''
> > parametert 'abcdef'
> > + echo 'var offset '\''cdef'\'''
> > var offset 'cdef'
> > + echo 'var noffset '\''ef'\'''
> > var noffset 'ef'
> > + echo 'val 2 '\''cdef'\'''
> > val 2 'cdef'
> > + echo 'val -2 (bad form) '\''abcdef'\'''
> > val -2 (bad form) 'abcdef'
> > + echo 'val -2 (good form) '\''ef'\'''
> > val -2 (good form) 'ef'
> > jim@jim-VirtualBox:~/project/busybox$
> >
> >
> >
> >
> > _______________________________________________
> > busybox mailing list
> > busybox@busybox.net
> > http://lists.busybox.net/mailman/listinfo/busybox
>
[Attachment #5 (text/html)]
<div dir="ltr"><div>Pulled from master, tested and confirmed to be fixed. \
Thanks.<br></div>-Jim<br></div><div class="gmail_extra"><br><div \
class="gmail_quote">On Sun, Jul 24, 2016 at 9:58 PM, Denys Vlasenko <span \
dir="ltr"><<a href="mailto:vda.linux@googlemail.com" \
target="_blank">vda.linux@googlemail.com</a>></span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Thanks for the report. Fixed in git, please try it.<br> \
<div><div class="h5"><br> On Fri, Jul 22, 2016 at 5:57 PM, James Hanley <<a \
href="mailto:jhanley@dgtlrift.com">jhanley@dgtlrift.com</a>> wrote:<br> > Since \
I'm unable to submit a bug via <a href="http://bugs.busybox.net" rel="noreferrer" \
target="_blank">bugs.busybox.net</a> due to some<br> > certificate error, I'm \
posting it here:<br> ><br>
><br>
> When offset is a literal negative the variable produces and error "bad<br>
> substitution" when issuing the proper form with a space after for the \
first<br> > : in ${parameter:offset:length} for a negative number. See<br>
> <a href="https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" \
rel="noreferrer" target="_blank">https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion</a><br>
> subsection on ${parameter:offset:length}<br>
><br>
> It should be noted that this does work when the offset parameter is a<br>
> variable instead of a literal with a negative number. The script below<br>
> reproduces the issue and showcases the operation with a variable offset vs a<br>
> value offset.<br>
><br>
> #!./busybox bash<br>
> set -x<br>
><br>
> parameter=abcdef<br>
> offset=2<br>
> noffset=-2<br>
><br>
> echo "parametert '${parameter}'"<br>
> echo "var offset '${parameter:${offset}}'"<br>
> echo "var noffset '${parameter:${noffset}}'"<br>
> echo "val 2 '${parameter:2}'"<br>
> echo "val -2 (bad form) '${parameter:-2}'"<br>
> echo "val -2 (good form) '${parameter: -2}'"<br>
> #EOF<br>
><br>
> With this script busybox bash gives the following output:<br>
> ~/project/busybox $ ./test.sh<br>
> + parameter=abcdef<br>
> + offset=2<br>
> + noffset=-2<br>
> + echo parametert 'abcdef'<br>
> parametert 'abcdef'<br>
> + echo var offset 'cdef'<br>
> var offset 'cdef'<br>
> + echo var noffset 'ef'<br>
> var noffset 'ef'<br>
> + echo val 2 'cdef'<br>
> val 2 'cdef'<br>
> + echo val -2 (bad form) 'abcdef'<br>
> val -2 (bad form) 'abcdef'<br>
> ./test.sh: line 13: syntax error: bad substitution<br>
> ~/project/busybox $<br>
><br>
> Changing the script to use the Linux Bash:<br>
> jim@jim-VirtualBox:~/project/busybox$ ./test.sh<br>
> + parameter=abcdef<br>
> + offset=2<br>
> + noffset=-2<br>
> + echo 'parametert \
'\''abcdef'\'''<br> > parametert \
'abcdef'<br> > + echo 'var offset \
'\''cdef'\'''<br> > var offset \
'cdef'<br> > + echo 'var noffset \
'\''ef'\'''<br> > var noffset \
'ef'<br> > + echo 'val 2 \
'\''cdef'\'''<br> > val 2 \
'cdef'<br> > + echo 'val -2 (bad form) \
'\''abcdef'\'''<br> > val -2 (bad form) \
'abcdef'<br> > + echo 'val -2 (good form) \
'\''ef'\'''<br> > val -2 (good form) 'ef'<br>
> jim@jim-VirtualBox:~/project/busybox$<br>
><br>
><br>
><br>
><br>
</div></div>> _______________________________________________<br>
> busybox mailing list<br>
> <a href="mailto:busybox@busybox.net">busybox@busybox.net</a><br>
> <a href="http://lists.busybox.net/mailman/listinfo/busybox" rel="noreferrer" \
target="_blank">http://lists.busybox.net/mailman/listinfo/busybox</a><br> \
</blockquote></div><br></div>
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic