如何在使用bash和eof的mysql 8中修复引号内的双引号/单引号?

khbbv19g  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(436)

如何修复此错误:


# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}

 > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
 > grant all privileges on zabbix.* to '\'zabbix'\'@'\'localhost'\' identified by '${ZABBIX_PASSWD}';
 > flush privileges;
 > exit
 > EOF
 mysql: [Warning] Using a password on the command line interface can be insecure.
 ERROR at line 2: Unknown command '\''.

或用双引号:


# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}

 > create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
 > grant all privileges on zabbix.* to "\""zabbix\""@"\"localhost"\"" identified by '${ZABBIX_PASSWD}';
 > flush privileges;
 > exit
 > EOF
 mysql: [Warning] Using a password on the command line interface can be insecure.
 ERROR at line 2: Unknown command '\"'.

不带双引号/单引号:


# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}

> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to zabbix@localhost identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 2: 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 'identified by ''' at line 1

或者只用双引号/单引号:


# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}

> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to "zabbix"@"localhost" identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 2: 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 'identified by ''' at line 1

eof shell文件中的错误是否相同?

tgabmvqs

tgabmvqs1#

这里不需要反斜杠。您混淆了herdeoc的命令行,反之亦然。您还混淆了mysql和命令行。其中的每一个(命令行、herdoc和mysql)都有关于单引号和双引号的不同规则。
mysql需要用单引号括起来的字符串文字(但也可以用双引号括起来,但这并不标准)。
bash显然有关于单引号和双引号的规则,但它们不适用于这里,因为这是一个heredoc
你的孩子不在乎。heredoc中的内容被视为一个文件。单引号,双引号,随便什么。最酷的是bash仍然会交换变量,所以它就像一个超级的,但它只是一个heredoc。
类似于以下的方法应该可以很好地工作:

cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
create database zabbix character set utf8 collate utf8_bin; create user 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
flush privileges;
exit
EOF

您的变量将被bash替换,即使它们周围有单引号,因为heredoc不在乎。这一切然后被传递到mysql,mysql很高兴,因为您的字符串文字被正确引用。
最后,如果你真的很喜欢那些双引号,你可以在你的heredoc中使用它们,这不会有什么区别。bash将忽略它们,mysql将允许它们通过。
我撒谎了,最后一件事。你可以用 <<- 在声明heredoc时,可以在heredoc中的行前面加空格,这样在脚本中执行此操作时更容易阅读。你也可以给你的herdeoc取任何名字,只要它以同一个词结尾(这两个都是为了脚本的清晰性/可读性)。你也不需要 cat 在这里,mysql可以直接从一个文件中使用,而herdeoc几乎在所有方面都是重要的,一个文件。

mysql -uroot -p${MYSQL_PASSWD} <<-MYSQLCOMMANDS
    create database zabbix character set utf8 collate utf8_bin; create user 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
    grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
    flush privileges;
    exit
MYSQLCOMMANDS

相关问题