From owner-freebsd-questions@FreeBSD.ORG Wed Jul 15 05:29:49 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8B1A31065672 for ; Wed, 15 Jul 2009 05:29:49 +0000 (UTC) (envelope-from bryanv@daemoninthecloset.org) Received: from misery.daemoninthecloset.org (misery.daemoninthecloset.org [216.66.9.70]) by mx1.freebsd.org (Postfix) with ESMTP id 624AF8FC13 for ; Wed, 15 Jul 2009 05:29:49 +0000 (UTC) (envelope-from bryanv@daemoninthecloset.org) Received: from bayleaf.daemoninthecloset.org (bayleaf.daemoninthecloset.org [24.206.126.92]) by misery.daemoninthecloset.org (Postfix) with ESMTP id C6FE4438105; Tue, 14 Jul 2009 22:17:02 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by bayleaf.daemoninthecloset.org (Postfix) with ESMTP id CA8FE4802D; Wed, 15 Jul 2009 00:02:17 -0500 (CDT) X-Virus-Scanned: amavisd-new at daemoninthecloset.org Received: from bayleaf.daemoninthecloset.org ([127.0.0.1]) by localhost (bayleaf.daemoninthecloset.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jAqVb1g8aT9E; Wed, 15 Jul 2009 00:02:16 -0500 (CDT) Received: from bayleaf.daemoninthecloset.org (bayleaf.daemoninthecloset.org [192.168.10.13]) by bayleaf.daemoninthecloset.org (Postfix) with ESMTP id B32114802C; Wed, 15 Jul 2009 00:02:16 -0500 (CDT) Date: Wed, 15 Jul 2009 00:02:16 -0500 (CDT) From: Bryan Venteicher To: Jay Hall Message-ID: <142219524.01247634136492.JavaMail.root@bayleaf> In-Reply-To: <4A48C83B-A36C-417F-9F68-F1CB1BCDDC8F@socket.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [192.168.10.20] X-Mailer: Zimbra 5.0.16_GA_2921.UBUNTU8_64 (zclient/5.0.16_GA_2921.UBUNTU8_64) Cc: freebsd-questions@freebsd.org Subject: Re: Bash and arrays X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jul 2009 05:29:49 -0000 ----- Jay Hall wrote: > Ladies and Gentlemen, > > I thought I understood how arrays work in bash, but I have been proven > wrong. I am reading lines from a file and placing them in an array. > However, when I am finished, the array has a length of 0. > > Following is the code I am using. > > #!/usr/local/bin/bash > COUNTER=0 > cat ./test_file.txt | while read LINE > do > echo ${LINE} > FOO[${COUNTER}]=${LINE} > COUNTER=`expr ${COUNTER} + 1` > done > > echo ${#FOO[@]} > echo ${#FOO[*]} > > > And, here is the output. > > test_file > file_size > 0 > 0 > > Thanks in advance for any help you can offer. The right hand side of the pipe is running in its own subshell so it has its own copy of FOO. One fix is #!/usr/local/bin/bash COUNTER=0 while read LINE do echo ${LINE} FOO[${COUNTER}]=${LINE} COUNTER=`expr ${COUNTER} + 1` done < ./test_file.txt > > > Jay > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"