kwksdy
kwksdy

Reputation: 91

Moving sqlite output into bash array

I am trying to load some sqlite3 output into an /n-delimited bash array. Seems like it should be a simple thing, but the read statement doesn't seem to produce any output. Echoing fqry just prior to the IFS statement confirms that the sqlite query is loading its output into a string correctly. But nothing appears to be getting into farray at all.

cmd="SELECT * FROM format"
fqry=`sqlite3 data.db "$cmd"`
IFS=$'\n' 
read -ra farray <<< "$fqry"
for f in "${farray[@]}"
do
echo "$f"
done

Upvotes: 4

Views: 2671

Answers (2)

hflzh
hflzh

Reputation: 644

You need to use the read -d option to define another delim, rather than the default newline character \n, to indicate the termination of the input line,

read -d '' -ra farray <<< "$fqry"

Upvotes: 2

mata
mata

Reputation: 69082

Use compound array assignment to create an indexed array instead of read:

cmd="SELECT * FROM format"
IFS=$'\n'
fqry=(`sqlite3 data.db "$cmd"`)

for f in "${fqry[@]}"; do
    echo "$f"
done

Upvotes: 4

Related Questions