How to use array values after the loop.

  • I m retreving values from database and wish to use those values later in my shell script. I m placing these values in an array da_data but outside loop array is empty.Problem is its treating array as local inside loop hence array is empty outside loop.

Plz go through the script and suggest how to use array values after the loop.

declare -a da_data

count=0
cat $LOAD_DA_FILE | \
while read da_name
do
da_data[count]=`$ORACLE_HOME/bin/sqlplus -s username/password@instance << EOF
set heading off
set feed off

select start_date from table where derive_name='$da_name';

EXIT
EOF`
count=`expr $count + 1`
echo ${da_data[count-1]} # displaying array value

done

echo -n "Elements Of array : "
echo ${da_data[@]} # array empty

  • thanks in advance

Try this instead:

declare -a da_data

count=0
while read da_name
do
da_data[count]=`$ORACLE_HOME/bin/sqlplus -s username/password@instance << EOF
set heading off
set feed off

select start_date from table where derive_name='$da_name';

EXIT
EOF`
count=`expr $count + 1`
echo ${da_data[count-1]} # displaying array value

done < $LOAD_DA_FILE

echo -n "Elements Of array : "
echo ${da_data[@]} # array empty

It prevents creation of a sub-process to handle the while/read loop, therefore the variables remain in the current scope.