我有一个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


我希望可以将数据库名称作为参数传递到MySQLCmds.txt中,以便在执行上述mysql命令时可以指定要删除的数据库和要创建的数据库。

这可能吗?

我在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的参数。

最佳答案

您可以将其替换为sed

userName=root

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

10-08 08:43
查看更多