将参数传递到MySQL文本文件中

时间:2015-10-01 03:44:26

标签: mysql bash shell

我有一个MySQLCmds.txt文件,其中包含一些MySQL命令:

drop database DB1;
create database DB1 character set=utf8 collate=utf8_bin;

drop database DB2;
create database DB2 character set=utf8 collate=utf8_bin;

执行MySQLCmds.txt的方式是

mysql -u$userName -hlocalhost < mysqlCmds_lyen.txt

我希望我可以将DB-names作为参数传递给MySQLCmds.txt,这样我就可以指定在执行上述mysql命令时要删除哪个DB以及要创建哪个DB。

这可能吗?

我在https://dev.mysql.com/doc/refman/5.6/en/mysql-batch-commands.html

找不到任何办法

我的意思是将MySQLCmds.txt修改成类似这样的东西

drop database $DB1;
create database $DB1 character set=utf8 collate=utf8_bin;

drop database $DB2;
create database $DB2 character set=utf8 collate=utf8_bin;

其中$ DB1和$ DB2是我传递给MySQLCmds.txt的参数。

1 个答案:

答案 0 :(得分:0)

您可以将其替换为 sed

userName=root

sed  -e 's/DB1/DBONE/g' -e 's/DB2/DBTWO/g'  MySQLCmds.txt | mysql -u$userName -hlocalhost
相关问题