'Store result of query in an array in shell scripting

I want to store row results of a query in array in unix scripting.

I tried this :

array=`sqlplus -s $DB <<eof
select customer_id from customer;
eof`;

When i tried to print it , it shows me this result:

echo ${array[0]};

CUSTOMER_ID ----------- 1 2 51 52 101 102 103 104 105 106 108 11 rows selected.

But I want to store each row as an element by excluding column_name and that "11 rows selected" sentence.

Thanks in Advance.



Solution 1:[1]

To create an array you need this syntax in BASH:

array=($(command))

or:

declare -a array=($(command))

For your sqlplus command:

array=($(sqlplus -s "$DB"<<eof
SET PAGESIZE 0;
select customer_id from customer;
eof))

and then print it as:

printf "%\n" "${array[@]}"

Just note that it is subject to all the shell expansions on white-spaces.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1