[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">&lt;<a href="mailto:vda.linux@googlemail.com" \
target="_blank">vda.linux@googlemail.com</a>&gt;</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 &lt;<a \
href="mailto:jhanley@dgtlrift.com">jhanley@dgtlrift.com</a>&gt; wrote:<br> &gt; Since \
I&#39;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> &gt; certificate error, I&#39;m \
posting it here:<br> &gt;<br>
&gt;<br>
&gt; When offset is a literal negative the variable produces and error &quot;bad<br>
&gt; substitution&quot; when issuing the proper form with a space after for the \
first<br> &gt; : in ${parameter:offset:length} for a negative number. See<br>
&gt; <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>
 &gt; subsection on ${parameter:offset:length}<br>
&gt;<br>
&gt; It should be noted that this does work when the offset parameter is a<br>
&gt; variable instead of a literal with a negative number.   The script below<br>
&gt; reproduces the issue and showcases the operation with a variable offset vs a<br>
&gt; value offset.<br>
&gt;<br>
&gt; #!./busybox bash<br>
&gt; set -x<br>
&gt;<br>
&gt; parameter=abcdef<br>
&gt; offset=2<br>
&gt; noffset=-2<br>
&gt;<br>
&gt; echo &quot;parametert              &#39;${parameter}&#39;&quot;<br>
&gt; echo &quot;var offset              &#39;${parameter:${offset}}&#39;&quot;<br>
&gt; echo &quot;var noffset            &#39;${parameter:${noffset}}&#39;&quot;<br>
&gt; echo &quot;val 2                     &#39;${parameter:2}&#39;&quot;<br>
&gt; echo &quot;val -2 (bad form)   &#39;${parameter:-2}&#39;&quot;<br>
&gt; echo &quot;val -2 (good form) &#39;${parameter: -2}&#39;&quot;<br>
&gt; #EOF<br>
&gt;<br>
&gt; With this script busybox bash gives the following output:<br>
&gt; ~/project/busybox $ ./test.sh<br>
&gt; + parameter=abcdef<br>
&gt; + offset=2<br>
&gt; + noffset=-2<br>
&gt; + echo parametert              &#39;abcdef&#39;<br>
&gt; parametert              &#39;abcdef&#39;<br>
&gt; + echo var offset              &#39;cdef&#39;<br>
&gt; var offset              &#39;cdef&#39;<br>
&gt; + echo var noffset            &#39;ef&#39;<br>
&gt; var noffset            &#39;ef&#39;<br>
&gt; + echo val 2                     &#39;cdef&#39;<br>
&gt; val 2                     &#39;cdef&#39;<br>
&gt; + echo val -2 (bad form)   &#39;abcdef&#39;<br>
&gt; val -2 (bad form)   &#39;abcdef&#39;<br>
&gt; ./test.sh: line 13: syntax error: bad substitution<br>
&gt; ~/project/busybox $<br>
&gt;<br>
&gt; Changing the script to use the Linux Bash:<br>
&gt; jim@jim-VirtualBox:~/project/busybox$ ./test.sh<br>
&gt; + parameter=abcdef<br>
&gt; + offset=2<br>
&gt; + noffset=-2<br>
&gt; + echo &#39;parametert              \
&#39;\&#39;&#39;abcdef&#39;\&#39;&#39;&#39;<br> &gt; parametert              \
&#39;abcdef&#39;<br> &gt; + echo &#39;var offset              \
&#39;\&#39;&#39;cdef&#39;\&#39;&#39;&#39;<br> &gt; var offset              \
&#39;cdef&#39;<br> &gt; + echo &#39;var noffset            \
&#39;\&#39;&#39;ef&#39;\&#39;&#39;&#39;<br> &gt; var noffset            \
&#39;ef&#39;<br> &gt; + echo &#39;val 2                     \
&#39;\&#39;&#39;cdef&#39;\&#39;&#39;&#39;<br> &gt; val 2                     \
&#39;cdef&#39;<br> &gt; + echo &#39;val -2 (bad form)   \
&#39;\&#39;&#39;abcdef&#39;\&#39;&#39;&#39;<br> &gt; val -2 (bad form)   \
&#39;abcdef&#39;<br> &gt; + echo &#39;val -2 (good form) \
&#39;\&#39;&#39;ef&#39;\&#39;&#39;&#39;<br> &gt; val -2 (good form) &#39;ef&#39;<br>
&gt; jim@jim-VirtualBox:~/project/busybox$<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div>&gt; _______________________________________________<br>
&gt; busybox mailing list<br>
&gt; <a href="mailto:busybox@busybox.net">busybox@busybox.net</a><br>
&gt; <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