How to store query multiple result in shell script variable(Array)

:slight_smile:
Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable.

like

#!/bin/ksh

v_shell_var[0]=Hi

here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array).

Regards,
Div

One way

#!/bin/ksh

let i=0
set -A arr 
USER=username
PWD=password

echo "
SELECT fld1 
 from TABLEA;
exit 
" | sqlplus -s $USER/$PWD | \
while read line
do
     arr($i)="$line"
     let i=$i+1
done

:b: You have given reply is very usefull. again thanks to you.

Just in Below code I have change array box with [] and add new code for print the array member.

#!/usr/bin/ksh

let i=0
#set -A arr

echo "select param_value
from control_param
where pgm_name = 'EXS_PROCESS';
exit
" | sqlplus -s ${ora_user}/${ora_passwd}@${ORACLE_SID} | \
while read line
do
if [[ $i -gt 2 ]]
then
arr[$i-3]="$line"
fi
let i=$i+1
done
echo "Array Variable is Filled Up"
i=0
echo " Total no. of member ${#arr[@]} "
echo "Print the Array member"
while [ $i -lt ${#arr[@]}-1 ]
do
print ${arr[$i]}
(( i=i+1 ))
done
echo "After Array Print"

Here, Situation is I have arr[] array. suppse
arr[0]=OWCD
arr[1]=SPCS contains
I want to if this pattern(arr[0] or arr[1]) match in ../Innovation dir's file then this file copy --> zip format --> into ../zipdir dir.
If pattern is not found then copy into ../archive dir area

I have tried below code...but not running. I feel , step 6/7 might be modified but how...please help me.

  1. i=0
  2. for j in `ls ../Innovation`
  3. do
  4. while [ $i -lt ${#arr[@]}-1 ]
  5. do
  6.  \# awk /$\{arr[$i]\}/ \{ \`cp $j ../archive\`; \} $j
    
  7.   source_dir=$\($j | head -1 | grep -i $\{arr[$i]\} | cp ../Innovation/$j ../archive\)
    
  8. done
    9.done

I need to get files names passed to a script. Here number of files passed may vary
like MyScript.ksh file1 file2 file3..... so on

I am writting script somthing like this

set -A Files

while (i<=$#)
do
File[i]=$i
let i=i+1
done

Is this correct way doing this. Is there any other way.
Please let me know

Thanks.