具有MySQL查询和变量的Bash脚本

时间:2018-07-23 09:31:34

标签: mysql bash variables

我正在尝试编写一个小的bash脚本,该脚本创建一些mysql表,然后将其分配给新用户:

[...]

DBUser='UserA'
VarA='BlaA'
VarB='BlaB'
VarC='BlaC'

[...]

MYSQL_PWD=`cat /etc/psa/.psa.shadow` mysql -uadmin <<MYSQL_SCRIPT
create database `${VarA}` character set = 'utf8';
create database `${VarB}` character set = 'utf8';
create database `${VarC}` character set = 'utf8';
create user 'seafile'@'localhost' identified by '${DBUser}';
SET PASSWORD FOR '${DBUser}'@'localhost' = '${dbpw}';
GRANT ALL PRIVILEGES ON `${VarA}`.* to `${DBUser}`@localhost;
GRANT ALL PRIVILEGES ON `${VarB}`.* to `${DBUser}`@localhost;
GRANT ALL PRIVILEGES ON `${VarC}`.* to `${DBUser}`@localhost;
MYSQL_SCRIPT

[...]

目前的结果是:

testinstall: line 93: BlaA: command not found
testinstall: line 93: BlaB: command not found
testinstall: line 93: BlaC: command not found
testinstall: line 93: BlaA: command not found
testinstall: line 93: BlaB: command not found
testinstall: line 93: BlaC: command not found
testinstall: line 93: UserA: command not found
testinstall: line 93: BlaC: command not found
testinstall: line 93: UserA: command not found
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character set = 'utf8'' at line 1

如何正确设置MYSQL_SCRIPT部分以使用变量?

编辑:谢谢@AndrewBone谢谢,命令问题没有通过删除“`”而消失。现在我仍然遇到“字符集='utf8'”的问题

#create database ${VarA} character set = 'utf8';
#ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-db character set = utf8' at line 1

如果我直接在mysql上运行,它将起作用:

create database `bla1` character set = 'utf8';
#Query OK, 1 row affected (0.00 sec)

关于如何解决它的任何想法?

谢谢。

0 个答案:

没有答案