Pawel Miszkurka Pawel Miszkurka - 1 year ago 92
Bash Question

BASH: Assigning MySQL SELECT result elements to different variables

Perhaps I am overcomplicating but ,I'm have built the following statement:

query="SELECT url, users FROM result as r INNER JOIN search as s ON s.UID = r.SearchID WHERE s.isProcessed = 0

The next step is to loop though the result but I need to be able to call urls and users separately. That's how I am attempting it:

while read -r link users; do
output+=("$link", "$users")
done < <(mysql -h $host -u $user -p$pwd -s -N -D $db -e "$query")

for i in ${!output[@]} ; do
echo "${output[i]}"

Any hint how to echo users only, please?
The above will return both urls and users :(

Answer Source

The users are in the odd-numbered elements of $output, so start at 1 and increment the array index by 2.

for ((i=1; i < ${#output[@]}; i+=2)); do
    echo "${output[$i]}"