Linux :: Shell script to loop through data in MySQL (array troubles!)

Associate
Joined
19 Jun 2003
Posts
1,680
Location
West Yorks, UK
Hi all,
I am trying to write a shell script that will gather some rows from MySQL, and loop through (doing other commands, but that bit isn't important). However, i'm struggling to work out how to do multi-dimensional arrays.

Say my table has 3 fields:
Code:
id
username
password
And there are 10 rows in there at the moment. I want to loop get all rows from MySQL, then loop through, slotting the different fields into different commands. My code thus far looks like this:
Code:
users=(`echo 'SELECT * FROM users;' | mysql -u ${USERNAME} -h ${HOST} --password=${PASSWORD} -D ${DATABASE}`)

for d in "${users[@]}"; do
        echo ${d}
done
But as you might imagine, this just presents me with a big long list of data from each field (ie., it prints 30 lines). Can anyone help me out as to how to have it in a 2D array so that I can refer to ${d["username"]} or something like that?

Cheers,
Matt
 
bash doesn't support 2D arrays, as far as I know. Perl would be a better option for this, I guess.
 
Back
Top Bottom