[NTLUG:Discuss] bash shell array question...

Ken Frazer frazer at acm.org
Fri Mar 1 16:20:56 CST 2002


Fred,
Generally when you are looping and redirecting input to the loop, either 
through a

while
do
.
.
.
done < some file

or by piping as you have done here, the loop will run in a subshell which 
means any variables which are assigned values within the loop will be lost 
when the loop terminates.

Regards,
krf

At 09:12 AM 2/27/2002 -0600, you wrote:
>(Sorry - on the first attempt I cut out the discussion part - second try here)
>
>#! /bin/bash
>#
>makeList.sh
>#
>ServerList=$1
>WordList=$2
>
>declare -a S
>declare -a U
>
>Count=0
>cat $ServerList | while read Server User
>do
>         S[$Count]=$Server
>         U[$Count]=$User
>         Count=`expr $Count + 1`
>         echo "$Count"
>         echo "${#S[*]} ${#S[@]}"
>         ACount=$Count
>done
>echo "count : $Count"
>echo "number of elements : ${#S[*]} ${#S[@]}"
>echo "acount : $ACount"
>
>Leave=0
>echo "$Leave $Count"
>while test $Leave -lt $Count
>do
>         echo "$Leave: ${S[$Leave]} ${U[$Leave]}"
>         Leave=`expr $Leave + 1`
>done
>exit
>
>
>
>Victor Brilon wrote:
>
>>Can you post some code showing exactly what you mean? Sounds like
>>you're running into a variable scope problem maybe.
>>Victor
>>On 26-Feb-2002 Fred James wrote:
>>
>>>Within a while loop, strings are assigned to the elements of an
>>>array, and the index variable is incremented.
>>>
>>>Once the loop completes, the value of the index variable is zero.
>>>
>>>What is happening?
>>>
>>>-- ...make every program a filter...
>>>
>>>
>>>_______________________________________________
>>>http://www.ntlug.org/mailman/listinfo/discuss
>
>
>--
>...make every program a filter...
>
>
>_______________________________________________
>http://www.ntlug.org/mailman/listinfo/discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ntlug.org/pipermail/discuss/attachments/20020301/308912d2/attachment.html


More information about the Discuss mailing list