Afficher un message
Vieux 27/08/2006, 13h43   #8
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bourne Shell: scope of variables in while loop

2006-08-27, 13:57(+02), Michal Nazarewicz:
> "Chris F.A. Johnson" <cfajohnson@gmail.com> writes:
>
>> On 2006-08-26, Doug Ritschel wrote:
>>> i=0
>>> while read line; do
>>> i = `expr $i + 1`
>>> done < /etc/hosts
>>> echo $i

>>
>> Second, you were not running it (with the error fixed) in a Bourne
>> shell. If you were, you would still have seen '0'. In a Bourne
>> shell, the redirecion alone is enough to force a subshell.

>
> Are you sure?
>
> $ cat test.sh
> i=0
> while read line; do i=$(($i + 1)); done </etc/hosts
> echo $i
> $ ash test.sh
> 24
> $ bash test.sh
> 24
> $ zsh test.sh
> 24
>
>
> Any pointers to standards which support your claim?

[...]

Neither ash, bash or zsh are the Bourne shell. Your ash is not
the original ash as it supports $((...)). You can find a Bourne
shell as the /bin/sh of Solaris or some very old commercial
Unices.

The Bourne shell doesn't have $((...)), and there's no standard
specifying the Bourne shell.

In the Bourne shell, redirection meant fork. Newer shells use
some trick to work around that limitation (they save the
redirected fds (for restauration afterwards) by dupplicating
them on other fds before doing the redirections, which you could
do manually with the Bourne shell this way:
exec 3<&0 < /etc/hosts
while read line
do i=`expr "$i" + 1 3<&-`
done
exec <&3 3<&-
)


--
Stéphane
  Réponse avec citation
 
Page generated in 0,04737 seconds with 9 queries