From: Javier Montoya on
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=[]
grep "car" ${myfile} | while read line; do
input[i]="$line";
echo ${input[$i]}
i=$((i + 1))
done
echo ${input[0]}

Best
From: superpollo on
Javier Montoya ha scritto:
> 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=[]
> grep "car" ${myfile} | while read line; do
> input[i]="$line";
> echo ${input[$i]}
> i=$((i + 1))
> done
> echo ${input[0]}
>
> Best

i think while loop limits the variable scope to the loop.

try this instead:

$ cat cus.in
car
mause
car2
car3

car001
$ cat cus.sh
#!/usr/bin/env bash
input=[]
input=$(grep "car")
echo ${input}
$ ./cus.sh < cus.in
car car2 car3 car001
$
From: Janis Papanagnou 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?

The shell you are using performs the command after the pipe in a
subshell, so variables are not seen in the shell outer context.

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;

grep ... | { while ... do ... done ; echo ... ; }

>
> i=0
> input=[]
> grep "car" ${myfile} | while read line; do
> input[i]="$line";
> echo ${input[$i]}
> i=$((i + 1))
> done
> echo ${input[0]}
>
> Best

(One additional note; your code (grep/while loop) already looks like
you'd be better off if using, e.g., awk for whatever task you try to
implement here.)

Janis
From: Teemu Likonen on
* 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")
From: Ben Finney on
Javier Montoya <jmontoyaz(a)gmail.com> writes:

> 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?

If you are willing to constrain the program to the Bash interpreter
version 4 or above, you can use the 'mapfile' builtin:

$ help mapfile
mapfile: mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into an indexed array variable.

Read lines from the standard input into the indexed array variable
ARRAY, or from file descriptor FD if the -u option is supplied. The
variable MAPFILE is the default ARRAY.
[…]

--
\ “The best way to get information on Usenet is not to ask a |
`\ question, but to post the wrong information.” —Aahz |
_o__) |
Ben Finney