2014-10-06 40 views
0

我有一个名为'sql'的文件夹,其中包含.sql文件。我想编写一个脚本来搜索所有.sql文件,将文件名放入一个数组中,然后开始每个文件。将文件存储到数组目录中,然后循环访问数组

即:

#!/bin/bash 

# Options 
DBHOST=MySQL-hostname 
DBNAME=MySQL-database 
DBUSER=MySQL-username 
DBPASS=MySQL-password 

# Find .sql Files 
??? 

# Create MySQL Tables 
for i in "${TBNAME[@]}"; do 
    mysql -h "$DBHOST" -u "$DBUSER" -p"$DBPASS" "$DBNAME" < $TBNAME[$i] 
done 

我如何搜索.sql文件指定的文件夹中?

回答

1
for sqlfile in sql/*.sql ; do 
    # do things to $sqlfile 
done 

例如,您可以通过使用echo "$PWD/$sqlfile"

+0

更简单!非常感谢你的帮助! – Mark 2014-10-06 15:46:41

0

晚aswer保存在一个表中的完整路径(后接受了一个),但备案:

printf "source %s\n" sql/**/*.sql | mysql --batch 

为此,你需要有在~/.bash_profile行:

shopt -s globstar #or put this before the above line 

工作原理:

printf "source %s\n" sql/**/*.sql 

产生像

source sql/some/file.sql 
source sql/other/file2.sql 
#and such... 

递归找到的所有* .sql文件的线条,以及

mysql --batch .... other arguments 

将阅读 “源文件名” 线 - 在执行SQL命令这些文件全部在一次执行中,不需要多次启动(运行)mysql命令...

相关问题