On 11.02.2013 18:50, Dashing wrote:
Bash version: 4.2.042

I have a script that behaves erratically:
=========
#! /bin/bash
last=${1##* }
rest=${1% *}
while [[ "${rest: -1}" == '\' ]]; do
        last="${rest##* } $last"
        oldrest=$rest
        rest=${rest% *}
        if [[ "$oldrest" == "$rest" ]]; then
                echo :--Mistake--:
#               sleep 0.01
#               rest=${rest% *}
#               if [[ "$oldrest" == "$rest" ]]; then
#                       echo 'unable to interpret'
#                       break
#               fi
        fi
done
echo REST:$rest:
echo LAST:$last:
=========
$ ./pe 'mplayer foo1\ foo2\ foo3\ 4\ 5\ foo6\ 7'
:--Mistake--:
:--Mistake--:
REST:mplayer:
LAST:foo1\ foo1\ foo1\ foo2\ foo3\ 4\ 5\ foo6\ 7:
=========

What happens is that rest=${rest% *} doesn't always update $rest,
even when there are spaces left in $rest.
Meanwhile last="${rest##* } $last" works every time.
In the above example it got stuck twice when $rest was mplayer
foo1\. It tends to vary wildly.

The script runs fine if you omit the superfluous blank in the ${rest: -1} expression:

        while [[ "${rest:-1}" == '\' ]]; do

If the script behaves indeterministic, it is probably a bash bug.

Regards,
Bernd



--
www.sudrala.de

Reply via email to