简体   繁体   English

在bash循环中使用2个数组从命令行查询mysql

[英]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: 我有2个数组:

1) An array that holds different database(db) names 1)包含不同数据库(db)名称的数组

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

2) An array that holds associated table names for each db 2)一个数组,其中包含每个数据库的关联表名

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. 另外,两个数组的顺序是这样的,我知道table1在db1中,而table2在db2中,依此类推。

I want to query mysql in a loop and pull several columns from each db-table pairing. 我想循环查询mysql,并从每个db-table配对中拉出几列。

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. 在下面的示例中,我仅使用数据库名称“ db4”而不是数据库数组。 Essentially, I want to replace the static "db4" with the array for databases. 本质上,我想用数据库数组替换静态“ db4”。

So, using the static db instead of the db array, this works: 因此,使用静态db而不是db数组,可以正常工作:

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. 这将导致以下一行一行地逐步增加每个数组中的下一个表和关联的db,直到数组中的所有项目完成循环为止。 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). 现在,让我们说每个数组中有3个项目(这2个数组中的db-to-tables将始终正确配对和排序)。

  1. do mysqlshortcut -e "select col1,col2,col3 from table1 order by rand() limit 1 ;" 做mysqlshortcut -e“按rand()限制1从table1的顺序选择col1,col2,col3;” db1 db1

  2. do mysqlshortcut -e "select col1,col2,col3 from table2 order by rand() limit 1 ;" 做mysqlshortcut -e“按rand()限制1从table2顺序选择col1,col2,col3;” db2 db2

  3. do mysqlshortcut -e "select col1,col2,col3 from table3 order by rand() limit 1 ;" 做mysqlshortcut -e“按rand()限制1从table3顺序选择col1,col2,col3;” db3 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. 我试图采用这种方法(上述)并将其应用于sql查询(在下面的示例中),但是它不起作用,而且我认为我没有正确构建它。 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. 我是bash脚本的新手,并且是数组的新手。

Thanks so much for any advice! 非常感谢您的任何建议!

'%s' is printf specific specifier, try using actual array expressions instead: '%s'是printf指定符,请尝试使用实际的数组表达式:

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

or use printf: 或使用printf:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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