Re: Problem with brace expansion

2015-04-23 Thread Greg Wooledge
On Thu, Apr 23, 2015 at 01:01:43AM +0100, Dr Alun J. Carr wrote:
> Well, to be strictly conformant with heirloom SysV sh,

Nobody cares about compatibility with Bourne shell any more.  Not since
Solaris finally pulled its head out of its ass and put a POSIX sh in
/bin (20 years after everyone else).

> Perhaps this needs to go into a FAQ somewhere about coding loops for an
> absolutely bare-bones shell.

You are on bug-bash, so all we really care about is bash.  Or maybe
POSIX.  Or more specifically, bash's conformance with POSIX.

If you want a FAQ, though, I have one for this:

http://mywiki.wooledge.org/BashPitfalls#pf33

OK, technically it's not in the FAQ, but close enough.



Re: Problem with brace expansion

2015-04-22 Thread Dr Alun J. Carr
Well, to be strictly conformant with heirloom SysV sh, we have to use the 
following (which is uglier than Cobol with a hangover):

#looper2-sh.sh
n=4
i=1
while [ $i -le $n ]
do
echo i = $i
i=`expr $i + 1`
done

which also works with dash and mksh. Perhaps this needs to go into a FAQ 
somewhere about coding loops for an absolutely bare-bones shell.

> On 22 Apr 2015, at 00:22, Eric Blake  wrote:
> 
> On 04/21/2015 01:44 PM, Dr Alun J. Carr wrote:
> 
>> Tests were done with bash, ksh, zsh, pdksh, dash and heirloom System V 
>> Bourne sh with the following versions:
>> bash 3.2.57(1)
>> bash 4.3.33(1)
>> ksh  version sh (AT&T Research) 93u+ 2012-08-01
>> zsh  5.0.5 (x86_64-apple-darwin14.0)
>> pdkshstable 5.2.14
>> dash stable 0.5.8
>> sh   ???
> 
> You do realize, I hope, that brace expansion is not required by POSIX,
> and therefore there is no standard for what behavior it has, and it
> should be no surprise that pdksh, dash, and Bourne sh do not try to
> expand it.
> 
> As for whether bash should expand $n into 4 prior to doing brace
> expansion, or whether brace expansion is attempted first but not
> recognizing $n as a valid bound devolves into no expansion, that is
> indeed a difference between ksh and bash.  But bash is consistent with
> its documentation:
> 
>>   The order of expansions is: brace expansion; tilde expansion, parameter
>>   and variable expansion, arithmetic expansion, and command  substitution
>>   (done  in a left-to-right fashion); word splitting; and pathname expan‐
>>   sion.
> 
> so you cannot use a variable as a bound and still expect brace expansion
> to work, since variables aren't expanded yet.
> 
> --
> Eric Blake   eblake redhat com+1-919-301-3266
> Libvirt virtualization library http://libvirt.org
> 



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: Problem with brace expansion

2015-04-21 Thread Eric Blake
On 04/21/2015 01:44 PM, Dr Alun J. Carr wrote:

> Tests were done with bash, ksh, zsh, pdksh, dash and heirloom System V Bourne 
> sh with the following versions:
> bash  3.2.57(1)
> bash  4.3.33(1)
> ksh   version sh (AT&T Research) 93u+ 2012-08-01
> zsh   5.0.5 (x86_64-apple-darwin14.0)
> pdksh stable 5.2.14
> dash  stable 0.5.8
> sh???

You do realize, I hope, that brace expansion is not required by POSIX,
and therefore there is no standard for what behavior it has, and it
should be no surprise that pdksh, dash, and Bourne sh do not try to
expand it.

As for whether bash should expand $n into 4 prior to doing brace
expansion, or whether brace expansion is attempted first but not
recognizing $n as a valid bound devolves into no expansion, that is
indeed a difference between ksh and bash.  But bash is consistent with
its documentation:

>The order of expansions is: brace expansion; tilde expansion, parameter
>and variable expansion, arithmetic expansion, and command  substitution
>(done  in a left-to-right fashion); word splitting; and pathname expan‐
>sion.

so you cannot use a variable as a bound and still expect brace expansion
to work, since variables aren't expanded yet.

-- 
Eric Blake   eblake redhat com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: Problem with brace expansion

2015-04-21 Thread Dennis Williamson
On Tue, Apr 21, 2015 at 2:44 PM, Dr Alun J. Carr 
wrote:

> There appears to be a bug in bash when using a variable in curly brace
> expansion, e.g., {1..$n}. I have put the two following test scripts in the
> attached files looper1.sh and looper2.sh:
>
> #looper1.sh
> for i in {1..4}
> do
> echo i = $i
> done
>
> #looper2.sh
> n=4
> for i in {1..$n}
> do
> echo i = $i
> done
>
> Tests were done with bash, ksh, zsh, pdksh, dash and heirloom System V
> Bourne sh with the following versions:
> bash3.2.57(1)
> bash4.3.33(1)
> ksh version sh (AT&T Research) 93u+ 2012-08-01
> zsh 5.0.5 (x86_64-apple-darwin14.0)
> pdksh   stable 5.2.14
> dashstable 0.5.8
> sh  ???
>
> Results for bash (both versions give the same result); note that bash
> fails to expand the curly brace expression in only the second case:
>
> $ bash looper1.sh
> i = 1
> i = 2
> i = 3
> i = 4
>
> $ bash looper2.sh
> i = {1..4}
>
> Repeating using ksh we get correct expansion of the curly braces:
>
> $ ksh looper1.sh
> i = 1
> i = 2
> i = 3
> i = 4
>
> $ ksh looper2.sh
> i = 1
> i = 2
> i = 3
> i = 4
>
> And using zsh, the same result as for ksh:
>
> $ zsh looper1.sh
> i = 1
> i = 2
> i = 3
> i = 4
>
> $ zsh looper2.sh
> i = 1
> i = 2
> i = 3
> i = 4
>
> Neither pdksh (which installs as ksh using homebrew) nor dash handle
> either case correctly:
>
> pdksh:
>
> $ /usr/local/bin/ksh looper1.sh
> i = {1..4}
>
> $ /usr/local/bin/ksh looper2.sh
> i = {1..4}
>
> dash:
>
> $ dash looper1.sh
> i = {1..4}
>
> $ dash looper2.sh
> i = {1..4}
>
> The System V sh from the heirloom project behaves the same way as pdksh
> and dash (or more correctly, since System V is really the reference, pdksh
> and dash behave the same way as SysV sh):
>
> $ 5 sh looper1.sh
> i = {1..4}
>
> $ 5 sh looper2.sh
> i = {1..4}
>
>
>

Bash performs brace expansion before variable expansion and thus does not
support the feature that zsh and ksh have which do variable expansion
before brace expansion.

To use a variable, use C-style for loops:

for ((i = 1; i <= 4; i++))

-- 
Visit serverfault.com to get your system administration questions answered.


Re: Problem with brace expansion

2015-04-21 Thread Chris F.A. Johnson

On Tue, 21 Apr 2015, Dr Alun J. Carr wrote:


There appears to be a bug in bash when using a variable in curly
brace expansion, e.g., {1..$n}. I have put the two following test
scripts in the attached files looper1.sh and looper2.sh:


  Brace expansion is done before variable expansion:

   The order of expansions is: brace expansion; tilde expansion,
   parameter and variable expansion, arithmetic expansion, and
   command substitution (done in a left-to-right fashion); word
   splitting; and pathname expansion.


Neither pdksh (which installs as ksh using homebrew) nor dash handle either 
case correctly:


  Neither pdksh nor dash has brace expansion.

--
Chris F.A. Johnson, 



Problem with brace expansion

2015-04-21 Thread Dr Alun J. Carr
There appears to be a bug in bash when using a variable in curly brace 
expansion, e.g., {1..$n}. I have put the two following test scripts in the 
attached files looper1.sh and looper2.sh:

#looper1.sh
for i in {1..4}
do
echo i = $i
done

#looper2.sh
n=4
for i in {1..$n}
do
echo i = $i
done

Tests were done with bash, ksh, zsh, pdksh, dash and heirloom System V Bourne 
sh with the following versions:
bash3.2.57(1)
bash4.3.33(1)
ksh version sh (AT&T Research) 93u+ 2012-08-01
zsh 5.0.5 (x86_64-apple-darwin14.0)
pdksh   stable 5.2.14
dashstable 0.5.8
sh  ???

Results for bash (both versions give the same result); note that bash fails to 
expand the curly brace expression in only the second case:

$ bash looper1.sh
i = 1
i = 2
i = 3
i = 4

$ bash looper2.sh
i = {1..4}

Repeating using ksh we get correct expansion of the curly braces:

$ ksh looper1.sh
i = 1
i = 2
i = 3
i = 4

$ ksh looper2.sh
i = 1
i = 2
i = 3
i = 4

And using zsh, the same result as for ksh:

$ zsh looper1.sh
  
i = 1
i = 2
i = 3
i = 4

$ zsh looper2.sh
i = 1
i = 2
i = 3
i = 4

Neither pdksh (which installs as ksh using homebrew) nor dash handle either 
case correctly:

pdksh:

$ /usr/local/bin/ksh looper1.sh
i = {1..4}

$ /usr/local/bin/ksh looper2.sh
i = {1..4}

dash:

$ dash looper1.sh
i = {1..4}

$ dash looper2.sh
i = {1..4}

The System V sh from the heirloom project behaves the same way as pdksh and 
dash (or more correctly, since System V is really the reference, pdksh and dash 
behave the same way as SysV sh):

$ 5 sh looper1.sh   
  
i = {1..4}

$ 5 sh looper2.sh
i = {1..4}




looper1.sh
Description: Binary data


looper2.sh
Description: Binary data