Reputation: 1
I'm trying to call a bash array into a psql query which is inside a for loop, but i can't make it to work...
This is what i have so far
#!/bin/bash
IFS=$'\n' groupids=(`psql -X -A -d files -t -c 'select id from groups where parent=127'`)
arr=${#groupids[@]}
for (( x = 0; x<arr; x++ )); do
users=$(psql -X -A -d files -t -c "select id from groups where parent=${groupids[${x}]}")
echo $users
done
The output is just blank lines.
Upvotes: 0
Views: 488
Reputation: 17226
Is it better?
#!/bin/bash
IFS=$'\n' read -r -d '' -a groupids < <(psql -X -A -d files -t -c 'select id from groups where parent=127')
for gid in "${groupids[@]}"
do
# check that we're dealing with digits
[[ $gid =~ ^[[:digit:]]+$ ]] || { echo "invalid format: $gid" >&2; continue; }
users=$(psql -X -A -d files -t -c "select id from groups where parent=$gid")
echo "$users"
done
Upvotes: 2