提问人:Vitor Mazuco 提问时间:10/9/2018 最后编辑:Vitor Mazuco 更新时间:10/9/2018 访问量:696
如何使用带有EOF的BASH修复MySQL 8中引号内的双引号/单引号?
How to fix a double/single quote inside the quotes in a MySQL 8 with BASH with EOF?
问:
如何修复此错误:
# 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 文件中的错误是否相同?
答:
2赞
JNevill
10/9/2018
#1
你在这里不需要反斜杠。您混淆了 heredoc 的命令行,反之亦然。您还混淆了 mysql 和命令行。其中每个(命令行、heredoc 和 mysql)对单引号和双引号都有不同的规则。
- Mysql需要用单引号括起来的字符串文字(但也可以使用双引号,但这不是标准的)。
- bash 显然有关于单引号和双引号的规则,但它们在这里不适用,因为这是一个 heredoc
- 你的heredoc不在乎。heredoc 中的内容被视为文件。单引号,双引号,随便什么。很酷的是 bash 仍然会交换变量,所以它就像一个 SUPERFILE,但它只是一个 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 中的行前面加上空格,如果您在脚本中执行此操作,则更容易阅读。你也可以随心所欲地命名你的heredoc,只要它以相同的单词结尾(这两者都是为了脚本的清晰度/可读性)。您也不需要这里,因为 mysql 可以直接从文件中使用,而 heredoc 几乎在所有重要方面都是文件。<<-
cat
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
评论
mysql
'
"