bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

contents of whole arrays dissapears leaving while read loop


From: Lennart Schultz
Subject: contents of whole arrays dissapears leaving while read loop
Date: Thu, 26 Mar 2009 08:21:00 +0100

Config info 1:
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: i686-pc-linux-gnu-gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686'
-DCONF_OSTYPE='linu
x-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -DCONF_VENDOR='pc'
-DLOCALEDIR='/usr/
share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I.
-I./include -I
./lib
-DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/
sbin:/bin' -DSTANDARD_UTILS_PATH='/bin:/usr/bin:/sbin:/usr/sbin'
-DSYS_BASHRC='/
etc/bash/bashrc' -DSYS_BASH_LOGOUT='/etc/bash/bash_logout'
-DNON_INTERACTIVE_LOG
IN_SHELLS -DSSH_SOURCE_BASHRC -march=athlon-xp -O2 -pipe
-fomit-frame-pointer -g
3
uname output: Linux dragoda.com 2.6.28-gentoo-r2 #1 SMP Sat Feb 28 19:17:31
CET
2009 i686 AMD Sempron(tm) 3000+ AuthenticAMD GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 4.0
Patch Level: 10
Release Status: release

Config info 2:
Configuration Information [Automatically generated, do not change]:
Machine: powerpc
OS: aix6.1.0.0
Compiler: cc -qlanglvl=extc89
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='powerpc'
-DCONF_OSTYPE='a
ix6.1.0.0' -DCONF_MACHTYPE='powerpc-ibm-aix6.1.0.0' -DCONF_VENDOR='ibm'
-DLOCALE
DIR='/usr/local/share/locale' -DPACKAGE='bash' -DSHELL  -DHAVE_CONFIG_H
-I.  -
I. -I./include -I./lib -I./lib/intl -I/home/lesc/src/bash/bash-4.0/lib/intl
-g
uname output: AIX fls-nim1 1 6 0001B6BAD300
Machine Type: powerpc-ibm-aix6.1.0.0

Bash Version: 4.0
Patch Level: 10
Release Status: release

Description:
In the construct
cat file|while read line
do
done
the content of any arry assignments in the loop dissapears leaving the loop:


Repeat-By:

Having a file named 'datafile' which have the tree lines:
one
two
tree

the bash code

declare -A numbers
counter=0
numbers[zero]=0
cat datafile|while read number
do
   counter=$((counter+1))
   numbers[$number]=$counter
   echo "${numbers[@]}"
done
echo "${numbers[@]}"

will generate the following output:
1 0
1 0 2
1 0 2 3
0

while the similar code

declare -A numbers
counter=0
numbers[zero]=0
while read number
do
   counter=$((counter+1))
   numbers[$number]=$counter
   echo "${numbers[@]}"
done < datafile
echo "${numbers[@]}"

will generate the (correct) output:

1 0
1 0 2
1 0 2 3
1 0 2 3

The first - but buggy - form will normally be prefrred while you often need
a filter like
egrep -v '^#'
in front of the while loop



-- 
Regards,
Lennart Schultz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]