From: Janis Papanagnou on
Teemu Likonen wrote:
> * 2010-04-30 17:12 (+0200), Janis Papanagnou wrote:
>
>> Either switch to a shell where that is not the case, e.g. original
>> Kornshells, or hack some workaround, like enclosing the relevant
>> parts in a compound command by using brackets;
>
> Or switch the order of processes:
>
> #!/bin/bash
> i=0
> while read -r line; do
> input[i++]=$line
> done < <(grep car "$myfile")

Yes. In case that your shell (and OS) supports process substitution.

Janis
From: Javier Montoya on
On Apr 30, 5:42 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> Teemu Likonen wrote:
> > * 2010-04-30 17:12 (+0200), Janis Papanagnou wrote:
>
> >> Either switch to a shell where that is not the case, e.g. original
> >> Kornshells, or hack some workaround, like enclosing the relevant
> >> parts in a compound command by using brackets;
>
> > Or switch the order of processes:
>
> >     #!/bin/bash
> >     i=0
> >     while read -r line; do
> >             input[i++]=$line
> >     done < <(grep car "$myfile")
>
> Yes. In case that your shell (and OS) supports process substitution.
>
> Janis

Hi guys,

Thanks for the whole support!

Best wishes
From: pk on
Javier Montoya wrote:

> Dear all,
>
> I've the following script below to read a given file. When I try to
> access a given element, once the loop has finished, the array seems to
> be empty. Any suggestions on that?
>
> i=0
> input=[]

You've got good answer for the variable in subshell problems - just note
that the above is not how you initialize an array.

In bash, you do either

declare -a input

or

input=()

(though I think strictly speaking they're slightly different)
From: Chris F.A. Johnson on
On 2010-04-30, Javier Montoya wrote:
> I've the following script below to read a given file. When I try to
> access a given element, once the loop has finished, the array seems to
> be empty. Any suggestions on that?
>
> i=0
> input=[]
> grep "car" ${myfile} | while read line; do
> input[i]="$line";
> echo ${input[$i]}
> i=$((i + 1))
> done
> echo ${input[0]}

IFS='
'
input=( $( grep car "$myfile" ) )


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====