[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Parameter Expansion] bug in ${variable% *}
From: |
Dashing |
Subject: |
[Parameter Expansion] bug in ${variable% *} |
Date: |
Mon, 11 Feb 2013 18:50:49 +0100 |
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.
I have commented out some code including sleep that sometimes helps
make the parameter expansion work. This commented out code also
avoids an infinite loop which is triggerable in a normal way with
an argument of:
command\ filename
Thanks,
Dashing