简体   繁体   中英

Using 2 arrays in a bash loop to query mysql from the command line

I've reviewed a lot of related posts and am still stuck, even after trying out recommended examples.

I have 2 arrays:

1) An array that holds different database(db) names

echo "${ARRAY_DB[*]}"
db1 db2 db3

2) An array that holds associated table names for each db

echo "${ARRAY_TABLE[*]}"
table1 table2 table3

For each db, all tables have the same structure of column names.

Also, the two arrays are in order such that I know table1 is in db1, and table2 is in db2, etc.

I want to query mysql in a loop and pull several columns from each db-table pairing.

Using just one array, I can loop successfully by using the array for the tables only. In the example below, I'm just using a name of a database,"db4" instead of the database array. Essentially, I want to replace the static "db4" with the array for databases.

So, using the static db instead of the db array, this works:

for i in "{$ARRAY_TRACK[@]}" ; do mysqlshortcut -e "select col1,col2,col3 from "$i" order by rand() limit 1 ;" db4 ; done

Conceptually, this is what I need to do:

for i in "{$ARRAY_TRACK[@]}" ; do mysqlshortcut -e "select col1,col2,col3 from "$i" order by rand() limit 1 ;" "{$ARRAY_DB[@]}" ; done

which would result in the following, line by line, incrementally adding the next table and associated db in each array until the total items in the array have completed the loop. For now let's say there are 3 items in each array (db-to-tables in those 2 arrays will always be paired and ordered correctly).

  1. do mysqlshortcut -e "select col1,col2,col3 from table1 order by rand() limit 1 ;" db1

  2. do mysqlshortcut -e "select col1,col2,col3 from table2 order by rand() limit 1 ;" db2

  3. do mysqlshortcut -e "select col1,col2,col3 from table3 order by rand() limit 1 ;" db3

After completing the loop above, my results would be something like this:

           col1 col2 col3
db1.table1  x    x    x
db2.table2  y    y    y 
db3.table3  z    z    z

I can actually do a simple loop using both of my arrays, like this:

for ((i=0;i<${#ARRAY_DB[@]};++i)); do printf "%s is in %s\n" "${ARRAY_TABLE[i]}" "${ARRAY_DB[i]}" ; done

Results are good:

table1 is in db1
table2 is in db2
table3 is in db3

I've tried to take this approach (above) and apply it to the sql query (in example below), but it doesn't work, and I don't think I've constructed it correctly. I have a feeling that I may be not understanding the quotes, among other things.

for ((i=0;i<${#ARRAY_DB[@]};++i)); do mysqlshortcut -e "select col1,col2,col3 from "%s";" %s "${ARRAY_DB[i]}" "${ARRAY_TRACK[i]}" ; done

Any suggestions, or other approaches? I'm new at bash scripting and am brand new to arrays.

Thanks so much for any advice!

'%s' is printf specific specifier, try using actual array expressions instead:

mysqlshortcut -e "select col1,col2,col3 from ${ARRAY_DB[i]}; ${ARRAY_TRACK[i]}"

or use printf:

 mysqlshortcut -e $(printf 'select col1,col2,col3 from %s; %s' "${ARRAY_DB[i]}" "${ARRAY_TRACK[i]}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM