'Bash script, get data from a DB and echo it

I have a script that search in a DB for a single column and store them in an array, then I use that column values in another query to get multiples columns, and store them in a variable.

Im trying to echo the variable message but when I run the script it doesnt echo anything.

#!/bin/bash

mapfile result < <(mysql -ugroot -p<pass> -D <db> -h  <host-ip> -P 3306 -s -N -e "SELECT <query> ;")

for row in "${result[@]}";do

message=`mysql -ugroot -p<pass> -D <db> -h  <host-ip> -P 3306 -s -N -e "SELECT <query>  AND tg2.nombre  = '${row}';"`

echo $message

done




Solution 1:[1]

Unless you explicitly need the result array I think you can use:

for row `mysql -ugroot -p<pass> -D <db> -h  <host-ip> -P 3306 -s -N -e "SELECT <query> ;"` ; do
    message=`mysql -ugroot -p<pass> -D <db> -h  <host-ip> -P 3306 -s -N -e "SELECT <query>  AND tg2.nombre  = '${row}';"`
    echo $message
done

BR

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 Alfredo Campos EnrĂ­quez