In bash, array element can notbe echoed outside the while loop
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]$