Bash从96个数据库中导出不同的表

时间:2021-11-26 15:24:33

first excuse me for my english i'm from switzerland :)

首先请原谅我的英语我来自瑞士:)

I need help with my Bash script to export tables with different prefixes from different mySQL databeses. I have to export a column named "introtext" from 96 databeses and in every db the tablenames have diefferent prefixes.

我需要帮助我的Bash脚本导出具有来自不同mySQL数据库的不同前缀的表。我必须从96个数据库中导出一个名为“introtext”的列,并且在每个数据库中,表名都有不同的前缀。

With my current script it only exports the first table from first db, my counter counts up but doesnt change the tablename and db...

使用我当前的脚本,它只从第一个db导出第一个表,我的计数器计数但没有更改tablename和db ...

The tablename and databasename in the variables are listed in the right/same order.

变量中的tablename和databasename按右/相同顺序列出。

Here my script with only 4 databeses from my test system.

这是我的脚本,我的测试系统只有4个数据库。

#!/bin/bash

#MySQL Login Variablen
user=root
pass=pwd123

#Tebellenname Variablen
STRtbl="ebn8r_content,h4akd_content,vbm6h_content,fb2tz_content"

#DB Variablen
STRDB="DB1,DB2,DB3,DB4"

counter=0
countermax=95

#IFS=',' read -a ARRAYdb <<< "$STRDB"
#IFS=',' read -a ARRAYtbl <<< "$STRtbl"
IFS=","
ARRAYdb=($STRDB)
ARRAYtbl=($STRtbl)

while [ $counter -le $countermax ]
do

#Entfernt Counter von Tabellenname/remove counter from tablename
tblcount=$ARRAYtbl[$counter]
tbl=${tblcount%[*}

#Entfernt Counter von DB-Namen/remove counter from dbname
dbcount=$ARRAYdb[$counter]
db=${dbcount%[*}
        echo "$dbcount"
#MySQL Abfrage Variable
query="SELECT introtext FROM $tbl INTO OUTFILE '/tmp/$db.csv' LINES TERMINATED BY '\n';"

#echo "$query"
        echo "Counter= $counter"
#Abfrage zusammenstellen und ausführen/build the full mysql query
for table in $(mysql -u$user -p$pass -D$db -Be "SHOW TABLES"); do
        echo "exporting $db"
mysql -u$user -p$pass -D$db -Be "$query"

counter=$[$counter +1]

        echo "$ARRAYdb[$counter]"
done
done
        echo "Export abgeschlossen"

1 个解决方案

#1


1  

To access an array element, you have to put {} around the variable:

要访问数组元素,您必须在变量周围添加{}:

tbl=${ARRAYtbl[$counter]}

db=${ARRAYdb[$counter]}

#1


1  

To access an array element, you have to put {} around the variable:

要访问数组元素,您必须在变量周围添加{}:

tbl=${ARRAYtbl[$counter]}

db=${ARRAYdb[$counter]}