Skip to Main Content

Infrastructure Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

In bash, array element can notbe echoed outside the while loop

NewLanderOct 25 2005 — edited Nov 29 2006
I have a following simple bash script which loops through the oratab to print out the database names. It puts database names into an array with index starting at 1.

[oracle@localhost oracle]$ cat b
#! /bin/bash

COUNTER=1
declare -ax SIDS

cat /etc/oratab | grep "^." | grep -v "^#" |grep -v "^*" | awk -F":" '{print $1 }' | while read SID
do
echo $COUNTER ")" $SID
SIDS[$COUNTER]=$SID
echo "${SIDS[$COUNTER]}"
COUNTER=$((COUNTER+1))
done

echo "${arr[1]}"

***** end of the code.

While running it, I had database names tiger and rcat echoed out inside the while loop. However, the last echo "${arr[1]}" did not print anything out. Looked like the value assigned inside the while loop was local to the loop. Outside the loop, it did not remember anything being assigned inside the loop. I tried to declaire the array with -ax since I read somewhere that -x was the same as exporting the array to make it global inside the script but it still did not work.

Please help out. This is really bothering me.

[oracle@localhost oracle]$ bash b
1 ) tiger
tiger ----- this was printed inside the loop
2 ) rcat
rcat ----- this was printed inside the loop
----- nothing was printed outside the loop here.
[oracle@localhost oracle]$
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 27 2006
Added on Oct 25 2005
11 comments
2,642 views