Bash scripting "while" problem

Giles Orr gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Thu Jun 6 02:10:52 UTC 2013


The code below appears to work, in that the debug echo command in the
middle of the while loop kicks out the responses I want.  But outside
the while loop the ip[] array is empty.  I've run into this before,
never really figured out why.  My wild guess without much
comprehension is that the while (and thus the array) is in a subshell
because of the pipe so the variable is never set in the script proper
...

So am I correct?  And how would I fix it?  Thanks.

index=0
ifconfig | grep "inet addr:" | awk '{ print $2 }' |
while read line
do
    tmp="${line}"
    tmp2="${tmp#addr:}"
    ip[${index}]="${tmp2%.?}"
    echo "ip[] is ${ip[${index}]}"
    index=$(($index+1))
done
echo "outside while, first ip is ${ip[0]}"

--
Giles
http://www.gilesorr.com/
gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list